Advanced array job in Java

I am trying to pass my ArrayList through Intent . But I can not find what to write additionally? Any receiving methods. I get errors such as "not applicable to 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 can not find how should I write to get an array of List?

 ArrayList<ItemDetails> child1 = getIntent().????????? 
+8
java android android-intent
source share
1 answer

But I can not find how I should write to get an array of List

Short answer: you cannot. You can only pass an ArrayList<String> and then get it with

 getIntent().getStringArrayListExtra("key"); 


But . If you want to pass custom objects through Intent, which your objects must implement:

You can choose one of them. Both work the same, but have different implementations.

Batch Interface:

If you choose the Parcelable interface, your ItemDetails class must implement Parcelable. Then you can specify it as

 intent.putParcelableArrayListExtra("key", value); 

and get it like:

 getIntent().getParcelableArrayListExtra("key"); 

I will not write you Parcelable, because it requires a bit more code. Here is a good example .

Serializable Interface:

If you choose the Serializable interface, I suggest you create a named class for the ItemDetailsWrapper instance that wraps your ArrayList(s)<ItemDetails>

Both elements of the ItemDetailsWrapper and ItemDetails class must implement the Serializable interface. Now you can pass this through Intent as follows:

 getIntent().putExtra("key", <serializableClass>); // storing getIntent().getSerializableExtra("key"); // retrieving 

Implementation Example:

 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 get through the steps:

 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(); 
+23
source share

All Articles