使用意图不起作用传递ArrayList

时间:2015-07-07 07:23:11

标签: android android-intent listadapter

我有一个列表适配器类,它加载服务数据并在自定义列表上膨胀。单击编辑按钮的图像需要将数据和意图加载到首选项屏幕。通过将对象替换为Parcelable,数据完全填充到Bundle,但抛出了marshal值异常。

适配器代码如下

imageEditIcon.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {

        InfoHandler infoHandler = new InfoHandler();
        infoList = infoHandler.load(context, pos);
        Intent intent = new Intent(context, ConfigurationActivity.class);
        Bundle dataBundle = new Bundle();
        dataBundle.putParcelableArrayList("InfoList", (ArrayList<? extends Parcelable>) InfoList);
        System.out.println("b4 pass "+dataBundle.toString());
//      Fragment fragment = new Fragment();
//      fragment.setArguments(dataBundle);
        intent.putExtras(dataBundle);
        context.startActivity(intent);
    }
});

1 个答案:

答案 0 :(得分:0)

你需要在你的班级中实现Parcelable:

//InfoList ?
public class MyClass implements Parcelable{

    private String someData;
    private int otherData;

    //you need to add a new constructor from a parcel source
    public MyClass(Parcel source){
        this.someData = source.readString();
        this.otherData = source.readInt();
    }

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

    @Override
    public void writeToParcel{Parcel dest , int flags){
         dest.wirteString(this.someData);
         dest.writeInt(this.otherData);
    }

    public static final Parcelable.Creator CREATOR = new Creator(){
        @Override
        public Object createFromParcel(Parcel source){
            return new Equipment(source);
        }

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

有关详情,here是官方文档!