Intent.putExtra List [duplicate]
Possible Duplicate:
How to put a List in intent
I want to pass a List from one activity to another. So far I have not been successful. This is my code.
//desserts.java
private List<Item> data;
@Override
public void onCreate(Bundle icicle) {
//Code
data.add(new Item(10, "dessert1"));
data.add(new Item(11, "dessert2"));
data.add(new Item(12, "dessert3"));
data.add(new Item(13, "dessert4"));
data.add(new Item(14, "dessert5"));
data.add(new Item(15, "dessert6"));
data.add(new Item(16, "dessert7"));
data.add(new Item(17, "dessert8"));
data.add(new Item(18, "dessert9"));
data.add(new Item(19, "dessert10"));
data.add(new Item(20, "dessert11"));
//Some more code
}
@Override
public void onClick(View v) {
Intent view_order_intent = new Intent(this, thirdpage.class);
view_order_intent.putExtra("data", data);
startActivity(view_order_intent);
}
But I am not able to put data this way. I asked this question earlier but not much happened.
Kindly help. Also help me how to get data in next activity.
Assuming that your List is a list of strings make data an ArrayList<String>
and use intent.putStringArrayListExtra("data", data)
Here is a skeleton of the code you need:
-
Declare List
private List<String> test;
-
Init List at appropriate place
test = new ArrayList<String>();
and add data as appropriate to
test
. -
Pass to intent as follows:
Intent intent = getIntent(); intent.putStringArrayListExtra("test", (ArrayList<String>) test);
-
Retrieve data as follows:
ArrayList<String> test = getIntent().getStringArrayListExtra("test");
Hope that helps.
If you use ArrayList instead of list then also your problem wil be solved. In your code only modify List into ArrayList.
private List<Item> data;