But i cannot find how am ı supposed to write to get arrayList
Short answer: you can't. You can only pass ArrayList<String>
and then retrieve it with
getIntent().getStringArrayListExtra("key");
But. If you want to pass custom objects via Intent your objects have to implement:
- Parcelable interface or Serializable interface
You can choose one of them. Both works same but have different implementations.
Parcelable interface:
If you choose Parcelable interface, your ItemDetails
class have to implement Parcelable. Then you can put it as
intent.putParcelableArrayListExtra("key", value);
and retrieve it as:
getIntent().getParcelableArrayListExtra("key");
I won't write you Parcelable implementation because it requires a little more code. Here is nice example.
Serializable interface:
If you choose Serializable interface i suggest you to create class named for instance ItemDetailsWrapper that will wrap your ArrayList(s)<ItemDetails>
Both i.e. ItemDetailsWrapper and ItemDetails class have to implement Serializable interface. Now you are able to pass it via Intent like this:
getIntent().putExtra("key", <serializableClass>); // storing
getIntent().getSerializableExtra("key"); // retrieving
Example of implementation:
public class ItemDetailsWrapper implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<ItemDetails> itemDetails;
public ItemDetailsWrapper(ArrayList<ItemDetails> items) {
this.itemDetails = items;
}
public ArrayList<ItemDetails> getItemDetails() {
return itemDetails;
}
}
public class ItemDetails implements Serializable {
private static final long serialVersionUID = 1L;
// getters, setters and properties
}
And how to pass through Activities:
ItemDetailsWrapper wrapper = new ItemDetailsWrapper(list);
Intent i = new Intent(<context>, <targetActivity>);
i.putExtra("obj", wrapper); // i.putExtra("obj", new ItemDetailsWrapper(list));
// retrieving
ItemDetailsWrapper wrap =
(ItemDetailsWrapper) getIntent().getSerializableExtra("obj");
ArrayList<ItemDetails> list = wrap.getItemDetails();