简体   繁体   中英

How to pass ArrayList of object between activities using Parcelable?

I want to send array list of object from one activity to another. For this I have extended my class with Parcelable.

Still I am not getting the list. Also I have sent a boolean variable for that also value dose not get change.

My object class:

public class contact implements Parcelable {

private String contactName;
private String contactId;

contact(){}

contact(String contactId,String contactName)
{
    this.contactId = contactId;
    this.contactName = contactName;

}

public contact(Parcel in) {

    contactId  = in.readString();
    contactName = in.readString();

}
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(contactId);
    dest.writeString(contactName);

}

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

public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

public String getContactName() {
    return contactName;
}

public void setContactName(String contactName) {
    this.contactName = contactName;
}

public String getContactid() {
    return contactId;
}

public void setContactid(String contactId) {
    this.contactId = contactId;
}

}

First activity :

   done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            Log.d("selectd",String.valueOf(selectedItemsPositions));

            mContactListActivity = true;

            selectedContacts = new ArrayList<>();//to store selected items

            for (Integer pos : selectedItemsPositions) {
                selectedContacts.add(items.get(pos));
            }

            Intent i = new Intent();

            Bundle bundle = new Bundle();

            bundle.putParcelableArrayList("selectedContacts",selectedContacts);
            bundle.putBoolean("contactListActivity",mContactListActivity);

            i.putExtras(bundle);

            setResult(RESULT_OK, i);

           finish();


        }
    });

}

Second Activity :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {

            Intent i = new Intent();

            Bundle bundle = new Bundle();

            mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");

            mContactListActivity = bundle.getBoolean("contactListActivity");

            if(mContactListActivity)
            {

                addOrganizer.setVisibility(View.INVISIBLE);

            }

            mAdapter.notifyDataSetChanged();

        }
    }
}

EDIT:

tried this as suggested by sush :

   Intent i = getIntent();

            Bundle bundle = i.getExtras();

            mSelectedContacts = (ArrayList<contact>)bundle.getSerializable("selectedContacts");

            mContactListActivity = bundle.getBoolean("contactListActivity",false);

It then throws null pointer exception on list.

What's going wrong?

UPDATE :

     mContactListActivity = true;

            selectedContacts = new ArrayList<>();//to store selected items

            for (Integer pos : selectedItemsPositions) {
                selectedContacts.add(items.get(pos));
            }

            Intent i = new Intent(ContactList.this,PlanEventActivity.class);

            i.putExtra("contactListActivity",mContactListActivity);
            i.putExtra("selectedContacts",(Serializable)selectedContacts);

            setResult(RESULT_OK, i);

           finish();

Second activity:

   Intent i = getIntent();


            mContactListActivity =  i.getBooleanExtra("contactListActivity",true);

mSelectedContacts = (ArrayList)i.getSerializableExtra("selectedContacts");

            Log.e("mSelectedContact ", String.valueOf(mSelectedContacts));
            mAdapter.notifyDataSetChanged();

Now I have done this , now boolean is getting pass but the list shows null.

     Bundle bundle = new Bundle(); // here ur creating new bundle and trying get data from empty bundle. 

 mSelectedContacts = bundle.getParcelableArrayList("selectedContacts"); 

     mContactListActivity = bundle.getBoolean("contactListActivity");








       Intent i = new Intent();

        Bundle bundle = new Bundle();

       /* bundle.putParcelableArrayList("selectedContacts",selectedContacts);
        bundle.putBoolean("contactListActivity",mContactListActivity);

        i.putExtras(bundle);
        */
       //try below code
        i.putParcelableArrayList("selectedContacts",selectedContacts);
        setResult(RESULT_OK, i);

for your code

 as per your code you should go like this

     Bundle b = i.getExtras();

Try Like This,

Intent updateIntent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.getParcelableArrayList("selectedContacts", selectedContacts);
updateIntent.putExtra(ContactsActivity.RECIVED_CONTACT, bundle);
startActivityForResult(updateIntent,1);

and Get like this on Second Activity's onActivityResult method

Bundle bundle = getIntent().getBundleExtra(ContactsActivity.RECEIVED_CONTACT);
ArrayList<contact> contact = bundle.getParcelableArrayList("selectedContacts");

Here is the problem: You are not getting bunble in second Activity, you are initialising a new bundle so you are getting null:

In second Activity:

           Bundle bundle =getIntent().getBundle();

            mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");

            mContactListActivity = bundle.getBoolean("contactListActivity");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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