gpt4 book ai didi

java - 在 Intent 之间发送自定义模型列表

转载 作者:行者123 更新时间:2023-11-29 20:26:33 25 4
gpt4 key购买 nike

我在 SO 上找到了很多答案,但没有一个对我有用。我有一个 List<MyModel> myList我想通过 Intents 传递和检索。
MyModel 实现了 Parcelable 及其实现。
我正在使用 intent.putExtra("my_key",(Parcelable) myList);
运行时抛出异常

java.util.ArrayList cannot be cast to android.os.Parcelable

如果我使用 putParcelableArrayListExtra然后它说第二个参数类型错误。
任何解释或其他方式都会有帮助
编辑:我的模型类是:

public class Filter {

private String categoryId;

public List<PrimaryFilterData> getPrimaryFilterDataList() {
return primaryFilterDataList;
}

public void setPrimaryFilterDataList(List<PrimaryFilterData> primaryFilterDataList) {
this.primaryFilterDataList = primaryFilterDataList;
}

public String getCategoryName() {
return categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public String getCategoryId() {
return categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

private String categoryName;

private List<PrimaryFilterData> primaryFilterDataList;


public static class PrimaryFilterData implements Parcelable
{
private String filterId;

protected PrimaryFilterData(Parcel in) {
filterId = in.readString();
filterName = in.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(filterId);
dest.writeString(filterName);
}

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

public static final Creator<PrimaryFilterData> CREATOR = new Creator<PrimaryFilterData>() {
@Override
public PrimaryFilterData createFromParcel(Parcel in) {
return new PrimaryFilterData(in);
}

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

public String getFilterName() {
return filterName;
}

public void setFilterName(String filterName) {
this.filterName = filterName;
}

public String getFilterId() {
return filterId;
}

public void setFilterId(String filterId) {
this.filterId = filterId;
}

public List<SecondaryFilterData> getSecondaryFilterDataList() {
return secondaryFilterDataList;
}

public void setSecondaryFilterDataList(List<SecondaryFilterData> secondaryFilterDataList) {
this.secondaryFilterDataList = secondaryFilterDataList;
}

private String filterName;

private List<SecondaryFilterData> secondaryFilterDataList;

}

public static class SecondaryFilterData implements Parcelable {
private String secFilterName;
private String secFilterId;
private boolean isChecked=false;

public SecondaryFilterData(){}

protected SecondaryFilterData(Parcel in) {
secFilterName = in.readString();
secFilterId = in.readString();
isChecked = in.readByte() != 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(secFilterName);
dest.writeString(secFilterId);
dest.writeByte((byte) (isChecked ? 1 : 0));
}

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

public static final Creator<SecondaryFilterData> CREATOR = new Creator<SecondaryFilterData>() {
@Override
public SecondaryFilterData createFromParcel(Parcel in) {
return new SecondaryFilterData(in);
}

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

public boolean isChecked() {
return isChecked;
}

public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}

public String getSecFilterName() {
return secFilterName;
}

public void setSecFilterName(String secFilterName) {
this.secFilterName = secFilterName;
}

public String getSecFilterId() {
return secFilterId;
}

public void setSecFilterId(String secFilterId) {
this.secFilterId = secFilterId;
}
}
}


现在我希望将一个 Activity 的主要数据列表发送给其他 Activity

最佳答案

您可以使用 Bundle 实现这一点。

// Important - use array list not regular list.
// MyModel must implement Parcelable
ArrayList<MyModel> myList;

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("my_key", myList);
intent.putExtras(bundle);

如果您的列表必须是 List 类型,您可以在最后一刻转换它。

bundle.putParcelableArrayList("my_key", new ArrayList<MyModel>(myList));

然后在接收 Activity 中。

List<MyModel> myList = getIntent().getParcelableArrayListExtra("my_key");

关于java - 在 Intent 之间发送自定义模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32449409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com