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

SAWD
  • 101
  • 1
  • 9
Jason Momoa
  • 123
  • 8
  • 2
    can you share code how to add data on list – Dharmender Manral Jun 05 '19 at 07:12
  • Yes sure, I'm using `ContentResolver` and `Cursor` to get contact data to contact list – Jason Momoa Jun 05 '19 at 07:19
  • Check your typecasting in SecondActivity. – Chandan Yadav Jun 05 '19 at 07:10
  • @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. – Deepak Kumar Jun 05 '19 at 07:37
  • @deepakkumar I tried to pass a String Variable from `FirstActivity` to `SecondActivity`, and this work perfectly but with ArrayList is no. – Jason Momoa Jun 05 '19 at 07:41
  • @jasonmomoa No, no I meant run the app in debug mode and see each variable state. See this documentation https://www.google.com/url?sa=t&source=web&rct=j&url=https://developer.android.com/studio/debug&ved=2ahUKEwiQ36Cx8NHiAhVGuo8KHQUzDaMQFjABegQIBxAM&usg=AOvVaw2nKjq6dxakMEtcZRPifU8w&cshid=1559721850359 – Deepak Kumar Jun 05 '19 at 08:04

4 Answers4

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");
Jolson Da Costa
  • 1,095
  • 1
  • 12
  • 31
0

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

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
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());
     }
Sabyasachi
  • 3,499
  • 2
  • 14
  • 21
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);


}
Varun
  • 214
  • 1
  • 5