Reputation: 163
I am trying to pass a custom ArrayList of objects between activites. I keep getting wrong argument errors. Read around on exisitng questions but none of them could resolve the issue.
Could you please point me in the right direction?
CODE BREAKDOWN:
public class object1 implements parcelable{
public void writeToParcel(Parcel dest, int flags) {
//things that need to be written
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public MoodEvent createFromParcel(Parcel in) {
return new MoodEvent(in);
}
public MoodEvent[] newArray(int size) {
return new MoodEvent[size];
}
};
public class list1{ //** "implements Parceblable" gives error**
private ArrayList<object1> ArrayList1, ArrayList2; //** Filters used on **ArrayList2
public void writeToParcel(Parcel out) {
out.writeTypedList(ArrayList1);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public MoodEvent createFromParcel(Parcel in) {
return new MoodEvent(in);
}
public MoodEvent[] newArray(int size) {
return new MoodEvent[size];
}
};
}
public class someActivity {
.
.
.
mapIntent = new Intent(oldActivity.this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(moodEventList); //**wrong argument types on both lines**
mapIntent.putParcelableArrayListExtra("mylist",moodEventList);
.
.
.
}
Upvotes: 0
Views: 191
Reputation: 2172
You can try the following code to send and receive custom arraylist of objects from one activity to another.
You can try the follwing code to send parsable arraylist with intent.
private List<ResourceTypeList> mResourceList;
public void sendParcableIntent()
{
Intent i=new Intent(ResourcePhoneBook.this,ResourceSort.class);
Bundle toSend = new Bundle();
toSend.putParcelableArrayList("Data", (ArrayList<? extends Parcelable>)mResourceList);
i.putExtras(toSend);
startActivity(i);
}
You can get parcelable arraylist with normal ArrayList format.
ArrayList mArraylist1;
Bundle bdl = getIntent().getExtras();
mArraylist1 = bdl.getParcelableArrayList("Data");
public class ResourceTypeList implements Parcelable {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("resource_type")
@Expose
private String resourceType;
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The resourceType
*/
public String getResourceType() {
return resourceType;
}
/**
*
* @param resourceType
* The resource_type
*/
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(resourceType);
dest.writeInt(id);
}
public ResourceTypeList(String resourceType) {
this.resourceType = resourceType;
}
protected ResourceTypeList(Parcel in) {
resourceType = in.readString();
id= in.readInt();
}
public static final Creator<ResourceTypeList> CREATOR = new Creator<ResourceTypeList>() {
@Override
public ResourceTypeList createFromParcel(Parcel source) {
return new ResourceTypeList(source);
}
@Override
public ResourceTypeList[] newArray(int size) {
return new ResourceTypeList[size];
}
};
}
Upvotes: 0
Reputation: 357
Try this
public class YOUR_CLASS_NAME implements Serializable
Passing data through intent using Serializable
Upvotes: 0
Reputation: 6495
putParcelableArrayList
will expect an ArrayList containing objects that implement the Parcelable
interface.
Assuming your object1 class correctly implements this, you can use just a regular ArrayList<object1>
instance and you don't need the custom list1
class.
If you really want to though, and your list1
class also implements the interface, you can use the putParcelable
method, since your list is just a single parcelable now.
Upvotes: 2