传递自定义数组对象列表

时间:2017-03-26 22:18:30

标签: java android

我正在尝试在活动之间传递对象的自定义ArrayList。我一直得到错误的参数错误。阅读现有问题,但没有一个能解决问题。

你能指点我正确的方向吗?

CODE BREAKDOWN:

public class object1 implements parcelable{
    public void writeToParcel(Parcel dest, int flags) {
    //things that need to be written

    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MoodEvent createFromParcel(Parcel in) {
            return new MoodEvent(in);
        }

        public MoodEvent[] newArray(int size) {
            return new MoodEvent[size];
        }
    };
public class list1{  //** "implements Parceblable" gives error**
      private ArrayList<object1> ArrayList1, ArrayList2; //** Filters used on **ArrayList2
      public void writeToParcel(Parcel out) {
        out.writeTypedList(ArrayList1);
    }


    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MoodEvent createFromParcel(Parcel in) {
            return new MoodEvent(in);
        }

        public MoodEvent[] newArray(int size) {
            return new MoodEvent[size];
        }
    };
}

public class someActivity {
.
.
.
mapIntent = new Intent(oldActivity.this, NewActivity.class);
Bundle bundle = new Bundle();

bundle.putParcelableArrayList(moodEventList);   //**wrong argument types on both lines**
mapIntent.putParcelableArrayListExtra("mylist",moodEventList);
.
.
.
}

3 个答案:

答案 0 :(得分:2)

putParcelableArrayList将期望包含实现Parcelable接口的对象的ArrayList 假设您的object1类正确实现了这一点,您只能使用常规ArrayList<object1>实例,并且您不需要自定义list1类。

如果您真的想要,并且您的list1类也实现了界面,您可以使用putParcelable方法,因为您的列表现在只是一个可分配的。

答案 1 :(得分:0)

试试这个

public class YOUR_CLASS_NAME implements Serializable

Passing data through intent using Serializable

答案 2 :(得分:0)

  

您可以尝试以下代码来发送和接收自定义的arraylist   从一个活动到另一个活动的对象。

发送意图值:

  

您可以尝试使用以下代码来发送可解析的arraylist。

 private List<ResourceTypeList> mResourceList;
 public void sendParcableIntent()
 {
    Intent i=new Intent(ResourcePhoneBook.this,ResourceSort.class);
    Bundle toSend = new Bundle();
    toSend.putParcelableArrayList("Data", (ArrayList<? extends Parcelable>)mResourceList);
    i.putExtras(toSend);
    startActivity(i);
 }

接收意图值:

  

您可以使用正常的ArrayList格式获得parcelable arraylist。

ArrayList mArraylist1;
Bundle bdl = getIntent().getExtras();
mArraylist1 = bdl.getParcelableArrayList("Data");

parcelable model class:

public class ResourceTypeList implements Parcelable {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("resource_type")
    @Expose
    private String resourceType;

    /**
     *
     * @return
     *     The id
     */
    public Integer getId() {
        return id;
    }

    /**
     *
     * @param id
     *     The id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     *
     * @return
     *     The resourceType
     */
    public String getResourceType() {
        return resourceType;
    }

    /**
     *
     * @param resourceType
     *     The resource_type
     */
    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(resourceType);
        dest.writeInt(id);

    }

   public ResourceTypeList(String resourceType) {
       this.resourceType = resourceType;

   }
   protected ResourceTypeList(Parcel in) {
       resourceType = in.readString();
       id= in.readInt();

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

        @Override
        public ResourceTypeList[] newArray(int size) {
            return new ResourceTypeList[size];
        }
    };
}
相关问题