I asked this question yesterday:
I have a problem. I want to add some items to my list view which is in activity 'b' by click on the button or other view in activity 'a'. For example:
My Activity 'a' :
b.setOnClickListener(new onClickListener() {
@Override
public void onClick(view v) {
// add item to myarray which there in activity 'b'
myArray.add("");
}
});
My Activity 'b' :
ArrayList<String> myArray;
Hope to get my purpose. Thank-you.
Then I get this answer:
You can create a third object ( A public class where you put the ArrayList ) and access it every time you need its values : Update it from first activity and get values from second activity.
Activity 1:
b.setOnClickListener(new onClickListener() {
@Override
public void onClick(view v) {
Utilities.addValue("String");
}
});
Activity 2:
ArrayList<String> myArray = Utilities.getArrayList();
Utility Class:
public static class Utilities {
static ArrayList<String> mArrayList;
public static void addValue(String a) {
if (null == mArrayList) {
mArrayList = new ArrayList<>();
}
mArrayList.add(a);
}
public static ArrayList<String> getArrayList() {
return mArrayList;
}
}
But I have a new problem. When I click on the button, item add to the array string and show in list view. But when I exit from app and comeback later to the app, Activity 'b' which there is the list view in it, crashed. What the problem? How to save added items? Thankyou.