【问题标题】:Android pass custom arrayList with bundleAndroid通过bundle传递自定义arrayList
【发布时间】:2016-12-03 12:56:09
【问题描述】:

这是我的数组列表:

 private List<myclass> arrList = new ArrayList<myclass>();

我的代码:

public class myclass implements Serializable {

private int user_id;
private String fname;
private String EditVal;

public myclass() {

}

public myclass(String fname, int user_id, String EditVal) {  
    this.user_id = user_id;
    this.fname = fname;
    this.EditVal = EditVal;
}


public String getFname(){
    return fname;
}

public String EditVal(){
    return EditVal;
}

}

现在在活动中,我想将项目添加到数组列表并使用 bundle 传递它

    Bundle outState = new Bundle ();

    myclass r = new myclass("medo medo",40, TxtVal);
    arrList.add(r);
    outState.putSerializable("savedList",  arrList);

但它给了我和错误

java.lang.RuntimeException: Parcelable 在写入可序列化对象时遇到 IOException

引起:java.io.NotSerializableException

【问题讨论】:

  • 你应该添加整个堆栈跟踪。

标签: android


【解决方案1】:

如果您自己的类实现了 SerializableParcelable,则可以传递您自己的类的 ArrayList。
这是因为它被序列化并“压缩”到包中。

Bundle outState = new Bundle ();

myclass r = new myclass("medo medo",40, TxtVal);
arrList.add(r);
// outState.putSerializable("savedList",  arrList);
outState.putParcelableArrayList("savedList", arraylist);

问候,

【讨论】:

  • 看看我用过的班级public class myclass implements Serializable
  • 是的,我看到了。我只是在评论中说它必须实现 Serializable 或 Parcelable 以便其他阅读该线程的人知道它是强制性的 :) 无论哪种方式,它都有效吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2014-06-19
  • 1970-01-01
相关资源
最近更新 更多