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");
Have you implement the object in ArrayList as Parcelable? I have to see the full code to help you.
ReplyDeleteITEM_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.
ReplyDeletegetting error in next page while extracting from list..
ReplyDeletefor (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 ...
What kind of error?
Deletekindly tell me about how to resolve the error of
ReplyDeleteThe method putParcelableArrayList(String, ArrayList) in the type Bundle is not applicable for the arguments (String, ArrayList)??????????
do you know we can send hashmap?
ReplyDeletehi can i use this to pass data between fragments?
ReplyDeletehow did you solve this?
ReplyDeleteandroid.os.TransactionTooLargeException: data parcel size 827860 bytes
ReplyDeletewhen I pass large Arraylist Using Parcelable .