【问题标题】:How to pass List of Objects between two activities in Android?如何在Android中的两个活动之间传递对象列表?
【发布时间】:2015-03-16 19:49:43
【问题描述】:

我正在尝试将对象列表从一个活动传递到另一个活动,这是我尝试过的,但出现错误:
错误:(61, 23) 错误:Bundle 类中的 putParcelable 方法不能应用于给定类型;必填:String,Parcelable 找到:String,List 原因:实参List无法通过方法调用转换为Parcelable

 private ListView listView1;
List <ListObject> listObjects = new ArrayList<ListObject>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
  listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Intent i = new Intent("com.tutorial.details");
           // startActivity(i);

            Intent intent = new Intent(getApplicationContext(),DetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelable("data", listObjects);
            intent.putExtras(bundle);
            startActivity(intent);

        }
    });
    ...... }

 class ListObject implements Parcelable{
    public String title;
    public String items;
    public String remaining;
    public ListObject(){
        super();
    }

    public ListObject(String title, String items, String remaining) {
        super();
        try {
            // this.icon = icon;
            this.title = title;
            this.items = items;
            this.remaining = remaining;
            if(Integer.parseInt(remaining)> Integer.parseInt(items)) {
                this.remaining = items;
                throw new IllegalArgumentException();
            }
        }catch(IllegalArgumentException e){
            System.out.println("items = "+ items + " remaining: "+remaining);
            e.printStackTrace();
        }
    }

    public ListObject(Parcel in)
    {

    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(title);
        parcel.writeString(items);
        parcel.writeString(remaining);
    }

    public int describeContents() {
        return 0;
    }

    public final Parcelable.Creator<ListObject> CREATOR = new Parcelable.Creator<ListObject>()
    {
        public ListObject createFromParcel(Parcel in)
        {
            return new ListObject(in);
        }
        public ListObject[] newArray(int size)
        {
            return new ListObject[size];
        }
    };

}

【问题讨论】:

    标签: android listactivity parcelable


    【解决方案1】:

    或者创建一个扩展ArrayList&lt;ListObject&gt;Parcelable 并将其传递到包中

    ListObjectList listObjectsList = new ListObjectList();
    ...
    bundle.putParcelable("data", listObjectList);
    

    ListObject

    class ListObject implements Parcelable{
        public String title;
        public String items;
        public String remaining;
    
        public ListObject(){
            super();
        }
    
        public ListObject(String title, String items, String remaining) {
            super();
            this.title = title;
            this.items = items;
            this.remaining = remaining;
        }
    
        // add getter and setter
    
        public ListObject(Parcel in) {
            this();
            readFromParcel(in);
        }
    
        private void readFromParcel(Parcel in) {
            this.title = in.readString();
            this.items = in.readString();
            this.remaining = in.readString();
        }
    
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeString(title);
            parcel.writeString(items);
            parcel.writeString(remaining);
        }
    
        public int describeContents() {
            return 0;
        }
    
        public final Parcelable.Creator<ListObject> CREATOR = new Parcelable.Creator<ListObject>()
        {
            public ListObject createFromParcel(Parcel in)
            {
                return new ListObject(in);
            }
            public ListObject[] newArray(int size)
            {
                return new ListObject[size];
            }
        };
    
    }
    

    ListObjectList

    public class ListObjectList extends ArrayList<ListObject> implements Parcelable {
    
        public ListObjectList() {
        }
    
        public ListObjectList(Parcel in) {
            this();
            readFromParcel(in);
        }
    
        private void readFromParcel(Parcel in) {
            this.clear();
            int size = in.readInt();
    
            for (int n = 0; n < size; n++) {
                ListObject item = new ListObject(in.readString(), in.readString(), in.readString());
                this.add(item);
            }
        }
    
        public int describeContents() {
            return 0;
        }
    
        public void writeToParcel(Parcel parcel, int flags) {
            int size = this.size();
            parcel.writeInt(size);
    
            for (int n = 0; n < size; n++) {
                ListObject item = this.get(n);
    
                parcel.writeString(item.getTitle());
                parcel.writeDouble(item.getItems());
                parcel.writeDouble(item.getRemaining());
            }
        }
    
        public final Parcelable.Creator<ListObjectList> CREATOR = new Parcelable.Creator<ListObjectList>() {
            public ListObjectList createFromParcel(Parcel in) {
                return new ListObjectList(in);
            }
    
            public ListObjectList[] newArray(int size) {
                return new ListObjectList[size];
            }
        };
    
    }
    

    【讨论】:

      【解决方案2】:

      一切看起来都不错,但我会让 ListObject 在活动之外成为它自己的类。另外你应该有:

      尝试使用:

      bundle.putParcelableArrayList("data", listObjects);
      

      然后改变:

        public ListObject(Parcel in)
       {
          title = in.readString();
          items = in.readString();
          remaining = in.readString();
       }
      

      【讨论】:

      • 我尝试了移动制作自己的课程的建议,但没有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多