如何在Android中的两个活动之间传递对象列表?

时间:2015-03-16 19:49:44

标签: android listactivity parcelable

我正在尝试将对象列表从一个活动传递到另一个活动,这是我尝试但我收到错误:
错误:(61,23)错误:类Bundle中的方法putParcelable不能应用于给定的类型; required:String,Parcelable found:String,List reason:实际参数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];
        }
    };

}

2 个答案:

答案 0 :(得分:1)

或者创建一个Parcelable扩展ArrayList<ListObject>并将其传递到

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

Class 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];
        }
    };

}

Class 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];
        }
    };

}

答案 1 :(得分:0)

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

尝试使用:

bundle.putParcelableArrayList("data", listObjects);

然后改变:

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