I'm having a problem when working with custom ArrayLists and trying to move them between activities. First I've declared a custom ArrayList in my first activity:
private ArrayList<String[]> dataArray = new ArrayList<String[]>();
Where each element of the ArrayList is an array of String elements.
Next, I put it into an intent with putExtra():
Intent int1 = new Intent(this, ManualModeActivity.class);
int1.putExtra(EXTRA_MESSAGE, dataArray);
Then, when I try to recover the ArrayList in my second Activity, I find that there's only a getStringArrayListExtra() method for ArrayList<String>
elements, so it doesn't work with ArrayList<String[]>
. I've been searching about this and all I find are examples with custom objects where the solution is to implement a Parcelable or Serialize interface on them. How could I do this with a "default" object like String[]? (Or maybe am I missing something about how interfaces works since I'm still a newbie in java?)
You can pass an ArrayList<E>
the same way, if the E type is Serializable
.
You would call the putExtra (String name, Serializable value)
of Intent to store, and getSerializableExtra (String name)
for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Your code for sending the data from the first activity is correct.
For receiving data in the second activity use intent
's getSerializableExtra()
method, something like:
ArrayList<String[]> data = (ArrayList<String[]>) getIntent().getSerializableExtra(EXTRA_MESSAGE);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.