使用Intent时传递数组数据为null

时间:2019-06-05 06:53:42

标签: android android-intent parcelable

我想在两个arraylist之间传递一个activity,但遇到问题

FirstActivity中,ArrayList不为空,但是在SecondActivity中,则为空,我不知道发生了什么事

我的意图

Intent intent = new Intent(mContext, NewContactActivity.class);
intent.putParcelableArrayListExtra("CONTACT_ARRAY", mData);
mContext.startActivity(intent);

我的第二活动

Intent intent = getIntent();
ArrayList<Contact> lstContact = this.getIntent().getParcelableArrayListExtra("CONTACT_ARRAY");
edtPhone.setText(lstContact.get(0).getPhone());

我的联系人课程

public class Contact implements Parcelable {
    private String id;
    private String Name;
    private String Fname;
    private String Phone;
    private String Email;


    public Contact(String id, String Name, String Fname, String Phone, String Email) {
        this.id = id;
        this.Name = Name;
        this.Fname = Fname;
        this.Email = Email;
        this.Phone = Phone;
    }

    public Contact(Parcel in) {
        this.id = in.readString();
        this.Name = in.readString();
        this.Fname = in.readString();
        this.Email = in.readString();
        this.Phone = in.readString();
    }

    //Getter
    public String getId() { return id; }

    public String getName() {
        return Name;
    }

    public String getFname() {
        return Fname;
    }

    public String getEmail() {
        return Email;
    }

    public String getPhone() {
        return Phone;
    }

    //Setter

    public void setName(String name) {
        this.Name = name;
    }

    public void setFname(String fname) {
        Fname = fname;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public void setPhone(String phone){ Phone = phone; }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(Name);
        dest.writeString(Fname);
        dest.writeString(Phone);
        dest.writeString(Email);

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

我该如何解决? 非常感谢

4 个答案:

答案 0 :(得分:1)

您必须传递arraylist而不是单个对象

RangeSet

在第二次活动中

ArrayList<Contact> lstContact = createContactList();

i.putParcelableArrayListExtra("CONTACT_ARRAY", (ArrayList) lstContact );

答案 1 :(得分:0)

this.getIntent()替换为intent。您根本没有使用它。

答案 2 :(得分:0)

尝试以下代码。

private void gotoNextActivity() {
    Intent intent=new Intent(this,NewContactActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("CONTACT_ARRAY", contactsList);
    intent.putExtras(bundle);
    startActivity(intent);
}

NewContactActivity.class

  ArrayList<BeanClass> listFromActivity1=new ArrayList<>(); 
  listFromActivity1=this.getIntent().getExtras().getParcelableArrayList("CONTACT_ARRAY");
  if (listFromActivity1 != null) {
       Log.d("Contact List",""+listFromActivity1.toString());
     }

答案 3 :(得分:0)

您必须在构造函数和writeToParcel函数中保持相同的顺序。

 @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id);
    dest.writeString(Name);
    dest.writeString(Fname);
    dest.writeString(Email);
    dest.writeString(Phone);


}