[英]Pass custom array list of objects
我试图在活动之间传递对象的自定义ArrayList。 我不断收到错误的参数错误。 阅读有关exisitng问题的信息,但没有一个可以解决问题。
您能指出我正确的方向吗?
代码分解:
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);
.
.
.
}
putParcelableArrayList
指望包含实现对象一个ArrayList Parcelable
接口。
假设您的object1类正确实现了此目的,则可以仅使用常规ArrayList<object1>
实例,而无需自定义list1
类。
如果确实需要,并且list1
类也实现了该接口,则可以使用putParcelable
方法,因为列表现在只是一个可打包的。
您可以尝试以下代码,以从一个活动向另一个活动发送和接收对象的自定义数组列表。
您可以尝试以下代码来发送具有意图的可解析数组列表。
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);
}
您可以使用常规ArrayList格式获取可拆分的arraylist。
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];
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.