Reputation: 15953
I have a java class that adds data to my sqlite db as follows:
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.
I then created a new activity with a button that would utilize addItem
from this class:
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
addItem
from my DataProvider
class but it did not work.I created a new method within the activity with the button to be used for the addItem
, still no luck.
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?
Upvotes: 1
Views: 142
Reputation: 192
just called static method of dataprovider and it worked like below
/**
* 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 :)
try sample project , its simple and working https://drive.google.com/open?id=0B2_EaCvs94pPeENtRlM4UURZYTQ
Upvotes: 1
Reputation: 2020
I am not sure why can't you use a java class' static method in a different activity.
My Suggestions-
1)Make Dataprovider class abstract and then let the main class of second activity extend it. 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. 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!!
Upvotes: 1
Reputation: 20162
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 */ )
Upvotes: 2
Reputation: 2856
You should try like creating separate method and calling that method in your OnClickListener
private void addItem(DataItem item) {
DataProvider.addItem(item)
}
Or Directly call that method from your OnClickListener like,
DataProvider.addItem(new DataItem(null, "Row 3", "Description 3", 3307));
And also check your import for addItem method
Upvotes: 1