[英]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. 为此,我使用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 : 按照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"); 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. 现在,我已完成此操作,现在布尔值已通过,但列表显示为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 然后在Second Activity的onActivityResult
方法上获取
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: 这是问题所在:您在第二个Activity中没有变得笨拙,正在初始化一个新的捆绑包,因此您将获得null:
In second Activity: 在第二个活动中:
Bundle bundle =getIntent().getBundle();
mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");
mContactListActivity = bundle.getBoolean("contactListActivity");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.