简体   繁体   English

传递对象的自定义数组列表

[英]Pass custom array list of objects

I am trying to pass a custom ArrayList of objects between activites. 我试图在活动之间传递对象的自定义ArrayList。 I keep getting wrong argument errors. 我不断收到错误的参数错误。 Read around on exisitng questions but none of them could resolve the issue. 阅读有关exisitng问题的信息,但没有一个可以解决问题。

Could you please point me in the right direction? 您能指出我正确的方向吗?

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);
.
.
.
}

putParcelableArrayList will expect an ArrayList containing objects that implement the Parcelable interface. putParcelableArrayList指望包含实现对象一个ArrayList Parcelable接口。
Assuming your object1 class correctly implements this, you can use just a regular ArrayList<object1> instance and you don't need the custom list1 class. 假设您的object1类正确实现了此目的,则可以仅使用常规ArrayList<object1>实例,而无需自定义list1类。

If you really want to though, and your list1 class also implements the interface, you can use the putParcelable method, since your list is just a single parcelable now. 如果确实需要,并且list1类也实现了该接口,则可以使用putParcelable方法,因为列表现在只是一个可打包的。

Try this 尝试这个

public class YOUR_CLASS_NAME implements Serializable

Passing data through intent using Serializable 使用Serializable通过意图传递数据

You can try the following code to send and receive custom arraylist of objects from one activity to another. 您可以尝试以下代码,以从一个活动向另一个活动发送和接收对象的自定义数组列表。

Send Intent Values: 发送意图值:

You can try the follwing code to send parsable arraylist with intent. 您可以尝试以下代码来发送具有意图的可解析数组列表。

 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);
 }

Receive Intent Values: 接收意图值:

You can get parcelable arraylist with normal ArrayList format. 您可以使用常规ArrayList格式获取可拆分的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];
        }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM