I am trying to skip my ArrayList
through Intent
. But I cannot find what to write in extra? Any get methods I am getting errors like "not applicable for string."
Item Details:
public class ItemDetails {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
public int getVideoNumber() {
return videoNumber;
}
public void setVideoNumber(int videoNumber) {
this.videoNumber = videoNumber;
}
public void setChild(ArrayList<ItemDetails> item_child)
{
this.item_child = item_child;
}
public ArrayList<ItemDetails> getChild()
{
return this.item_child;
}
public void setParent(ArrayList<ItemDetails> item_parent)
{
this.item_parent = item_parent;
}
public ArrayList<ItemDetails> getParent()
{
return this.item_parent;
}
private String name ;
private String itemDescription;
private String price;
private int imageNumber;
private int videoNumber;
private ArrayList<ItemDetails> item_child;
private ArrayList<ItemDetails> item_parent;
}
My class:
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
ArrayList<ItemDetails> item_parent;
ArrayList<ItemDetails> item_child;
}
My putExtra:
intObj.putExtra("exerciselist",obj_itemDetails.getChild());
GetChild function :
public ArrayList<ItemDetails> getChild()
{
return this.item_child;
}
But I cannot find how am I supposed to write to get arrayList?
ArrayList<ItemDetails> child1 = getIntent().?????????
Short answer: you can't. You can only pass
ArrayList<String>
and then retrieve it withBut. If you want to pass custom objects via Intent your objects have to implement:
Parcelable interface
orSerializable 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 asand retrieve it as:
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:
Example of implementation:
And how to pass through Activities: