简体   繁体   English

调用onClickListener的特定方法

[英]Calling a specific method onClickListener

I have a java class that adds data to my sqlite db as follows: 我有一个Java类,将数据添加到我的sqlite数据库中,如下所示:

public class DataProvider {
    public static List<DataItem> dataItemList;
    public static Map<String, DataItem> dataItemMap;

    static {
        dataItemList = new ArrayList<>();
        dataItemMap = new HashMap<>();

        addItem(new DataItem(null, "Row 1", "Description 1", 3107));
        addItem(new DataItem(null, "Row 2", "Description 2", 2519));
    }

    public static void addItem(DataItem item) {
        dataItemList.add(item);
        dataItemMap.put(item.getAccountId(), item);
    }

}

Where DataItem is a model class that handles data information. 其中, DataItem是处理数据信息的模型类。

I then created a new activity with a button that would utilize addItem from this class: 然后,我创建了一个带有按钮的新活动,该按钮将利用此类中的addItem

    Button addButton = (Button) findViewById(R.id.button_add);
    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            addItem(new DataItem(null, "Row 3", "Description 3", 3307));
            finish();
        }
    });

Attempt 尝试

  1. I tried by statically importing addItem from my DataProvider class but it did not work. 我尝试通过从DataProvider类中静态导入addItem来尝试,但是它没有用。
  2. I created a new method within the activity with the button to be used for the addItem , still no luck. 我在活动中创建了一个新方法,该按钮将用于addItem ,但还是没有运气。

     private void addItem(DataItem item) { List<DataItem> dataItemList; Map<String, DataItem> dataItemMap; dataItemList = new ArrayList<>(); dataItemMap = new HashMap<>(); dataItemList.add(item); dataItemMap.put(item.getAccountId(), item); } 

Question

How else am I able to import addItem to be able to use it within onClick method inside my second activity? 我还如何导入addItem以能够在第二个活动的onClick方法中使用它?

As I see Your data structure is static so it can be used everywhere after importing it. 如我所见,您的数据结构是静态的,因此导入后可以在任何地方使用。 It is no need to create any methods in controller, data provider can be used directly everywhere so also in listener : 无需在控制器中创建任何方法,数据提供者可以直接在任何地方使用,因此也可以在侦听器中使用:

DataProvider.addItem (/* new item */ )

You should try like creating separate method and calling that method in your OnClickListener 您应该尝试创建单独的方法并在OnClickListener中调用该方法

private void addItem(DataItem item) {
    DataProvider.addItem(item)
}

Or Directly call that method from your OnClickListener like, 或者直接从您的OnClickListener调用该方法,例如,

DataProvider.addItem(new DataItem(null, "Row 3", "Description 3", 3307));

And also check your import for addItem method 并检查您的导入是否有addItem方法

I am not sure why can't you use a java class' static method in a different activity. 我不确定为什么您不能在其他活动中使用Java类的静态方法。

My Suggestions- 我的建议

1)Make Dataprovider class abstract and then let the main class of second activity extend it. 1)使Dataprovider类抽象,然后让第二个活动的主类对其进行扩展。 Then just override the method there in second activity. 然后只需在第二个活动中覆盖那里的方法即可。

2) Make an instance of the Dataprovider class in the first activity then let your Dataprovider class implement Serializable interface. 2)在第一个活动中创建Dataprovider类的实例,然后让您的Dataprovider类实现Serializable接口。 Then you can send your object along with whatever intent thats calling your second activity. 然后,您可以发送对象以及调用第二个活动的任何意图。 Try this- 尝试这个-

    // send where details is object
ClassName details = new ClassName();
Intent i = new Intent(context, EditActivity.class);
i.putExtra("Editing", details);
startActivity(i);


//receive
ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");

And 

Class ClassName implements Serializable {
} 

Hope this helps!! 希望这可以帮助!!

just called static method of dataprovider and it worked like below 只是称为dataprovider的静态方法,它的工作方式如下

/**
 * method which is called on click 
 * i has reference present in fragment XML
 * 
 *  android:onClick="myMethod"
 * 
 * @param v
 */
public void myMethod(View v) {
    DataItem item = new DataItem(String.valueOf(new Random().nextInt()), String.valueOf(new Random().nextInt()), String.valueOf(new Random().nextInt()), new Random().nextInt());
    DataProvider.addItem(item );
}

however your data-provider is storing data in memory, not in DB , i think you put it as example :) 但是您的数据提供者将数据存储在内存中,而不是DB中,我认为您将其作为示例:)

try sample project , its simple and working https://drive.google.com/open?id=0B2_EaCvs94pPeENtRlM4UURZYTQ 尝试示例项目,它简单且有效https://drive.google.com/open?id=0B2_EaCvs94pPeENtRlM4UURZYTQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM