You can solve this problem in a couple of ways. From the moment that you don't have a static amount of data source, you need to use an ArrayList or something similar. You didn't specified, so I'll assume that all the data that you have is of type String.
First of all, you have to declare the ArrayList, like this:
ArrayList<String> arrayList = new ArrayList<>();
Now that you declared the list, you have to populate it. You can do it this way:
arrayList.add("New entry");
Now you have to pass the data between activity. For this, there are quite a few ways to do it. You have to choose the right way for you, here I'm going to show you two different ways, for two different cases. Please note that all the following solutions don't implement persistent data, so you'll lost every data when you close your app.
- If you need to access those data in more than one activity and in different moments, you can create an object and access it whenever you need.
Here an example:
public class YourDataManager{
private ArrayList<String> dataArrayList;
private static YourDataManager instance;
public YourDataManager(ArrayList<String> dataList){
instance = this;
dataArrayList = dataList;
}
public static YourDataManager getInstance(){
return instance;
}
public List<String> getData(){
return dataArrayList;
}
}
Now, in order to SET data, you have to do this:
//Where you have to set data. Assumed that the ArrayList with the data is already defined and populated. The name of that ArrayList from now on will be arrayList
YourDataManager dataManager = new YourDataManager(arrayList);
Now, to retrieve the data stored in YourDataManager, do the following:
//Retrieve data from YourDataManager
ArrayList<String> savedArrayList = YourDataManager.getInstance().getData();
//Now you have the arrayList
- The second method is similar to sending data using the intent. The limitation of this method is that you don't have it saved and accessible everywhere from your app. For this method, however, I'll redirect you to this StackOverflow answer.