Monday, February 1, 2010

Passing a list of objects between Activities

In Android, you can use Bundle to pass data between activities. There are many functions to pass Integer, String, etc. To pass an object, you will need to implement Parcelable interface and override some functions to serialize/de-serialize the object. Here is an example:
public class Category implements Parcelable {
private String category;
private ArrayList<Item> items;

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public ArrayList<Item> getItems() {
return items;
}

public void setItems(ArrayList<Item> items) {
this.items = items;
}

public Category(){

}

public Category(String _category){
category = _category;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(category);
Bundle b = new Bundle();
b.putParcelableArrayList("items", items);
dest.writeBundle(b);

}

public static final Parcelable.Creator<Category> CREATOR = 
new Parcelable.Creator<Category>() { 
public Category createFromParcel(Parcel in) { 
Category category = new Category();
category.category = in.readString();
Bundle b = in.readBundle(Item.class.getClassLoader());        
category.items = b.getParcelableArrayList("items");

return category;
}

@Override
public Category[] newArray(int size) {
return new Category[size];
}
};
}


To pass an arraylist  of Category to another activity,
intent i = new Intent(_av.getContext(), ItemList.class);
Bundle b = new Bundle();
b.putParcelableArrayList("categories", categories);
b.putInt("index", _index);
i.putExtras(b);
startActivityForResult(i, ITEM_LIST);

To retrieve the data,
Bundle b = this.getIntent().getExtras();

ArrayList<Category> cats = b.getParcelableArrayList("categories");
int index = b.getInt("index");

11 comments:

  1. Hi, I get everything from this great example but I have an error in doing

    b.putParcelableArrayList("categories", categories);

    It says:
    The method putParcelableArrayList(String, ArrayList) in the type Bundle is not applicable for the arguments (String, ArrayList)

    Thank you.

    ReplyDelete
  2. Have you implement the object in ArrayList as Parcelable? I have to see the full code to help you.

    ReplyDelete
  3. Than you dotNetIdes, i resolved this problem but now, what is the ITEM_LIST in the line startActivityForResult(i, ITEM_LIST); ?

    ReplyDelete
  4. ITEM_LIST is just a constant integer for requestCode parameter. This code will be returned in onActivityResult() when the activity exits. You can set it to any non-zero integer. You may check online for more info on startActivityForResult() method.

    ReplyDelete
  5. getting error in next page while extracting from list..
    for (int i = 0; i < lstAvtivityVo.size (); i++)
    {
    activityVO1 a = lstAvtivityVo.get (i);
    Log.d (TAG, "dte:" + a.getActDate());
    System.out.println(a.getActivityId() + " is from " + a.getActType());
    }

    lstlstAvtivityVo is a list which i got from previous page in intent.
    pls suggest me..

    how to retrive list data ...

    ReplyDelete
  6. kindly tell me about how to resolve the error of
    The method putParcelableArrayList(String, ArrayList) in the type Bundle is not applicable for the arguments (String, ArrayList)??????????

    ReplyDelete
  7. do you know we can send hashmap?

    ReplyDelete
  8. hi can i use this to pass data between fragments?

    ReplyDelete
  9. android.os.TransactionTooLargeException: data parcel size 827860 bytes
    when I pass large Arraylist Using Parcelable .

    ReplyDelete