0

I want to pass an arraylist between 2 activity but I met a problem

In FirstActivity the ArrayList is not null but In SecondActivity it's null and I don't know What is happened

My Intent:

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

My SecondActivity:

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

My Contact class:

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

How can I resolve this ? Thanks you so much

6
  • Check your typecasting in SecondActivity. Commented Jun 5, 2019 at 7:10
  • 2
    can you share code how to add data on list Commented Jun 5, 2019 at 7:12
  • Yes sure, I'm using ContentResolver and Cursor to get contact data to contact list Commented Jun 5, 2019 at 7:19
  • @jasonmomoa Did you tried to debug this by looking at what extra data you are sending through intent and what are you getting on the other activity? If none of the answers work you can try this, This helps me most of the time. Commented Jun 5, 2019 at 7:37
  • @deepakkumar I tried to pass a String Variable from FirstActivity to SecondActivity, and this work perfectly but with ArrayList is no. Commented Jun 5, 2019 at 7:41

4 Answers 4

1

You have to pass the arraylist and not single object

ArrayList<Contact> lstContact = createContactList();

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

while in second activity

 ciArr = (List) i.getParcelableArrayListExtra("CONTACT_ARRAY");
Sign up to request clarification or add additional context in comments.

Comments

0

Replace this.getIntent() with your intent. You are not using it at all.

1 Comment

are you sure your mData is not empty ?
0

You have to maintain the same order in your constructor and in writeToParcel function.

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


}

Comments

0

Try with below code.

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 Comments

@@ not working, This way is good but I don't know why it's still not working
Can you debug and see your arraylist is empty or not ?
in FirstActivity is not null, but in SecondActivity is null. And I don't know What is happened ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.