我想将对象的数组列表从一个活动发送到另一个活动。为此,我用Parcelable扩展了我的课程。
我仍然没有得到名单。此外,我还发送了一个布尔变量,该值也没有得到改变。
我的对象类:
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;
}
}
第一项活动:
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();
}
});
}
第二项活动:
@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();
}
}
}
编辑:
按照sush的建议尝试了这个:
Intent i = getIntent();
Bundle bundle = i.getExtras();
mSelectedContacts = (ArrayList<contact>)bundle.getSerializable("selectedContacts");
mContactListActivity = bundle.getBoolean("contactListActivity",false);
然后它会在列表上抛出空指针异常。
出了什么问题?
更新:
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();
第二项活动:
Intent i = getIntent();
mContactListActivity = i.getBooleanExtra("contactListActivity",true);
mSelectedContacts =(ArrayList)i.getSerializableExtra(“selectedContacts”);
Log.e("mSelectedContact ", String.valueOf(mSelectedContacts));
mAdapter.notifyDataSetChanged();
现在我已经完成了这个,现在boolean正在通过,但列表显示为null。
答案 0 :(得分:0)
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);
代码
as per your code you should go like this
Bundle b = i.getExtras();
答案 1 :(得分:0)
尝试这样,
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);
并在Second Activity的onActivityResult
方法
Bundle bundle = getIntent().getBundleExtra(ContactsActivity.RECEIVED_CONTACT);
ArrayList<contact> contact = bundle.getParcelableArrayList("selectedContacts");
答案 2 :(得分:0)
问题是:你没有在第二个Activity中获得bunble,你正在初始化一个新的bundle,所以你得到null:
在第二个活动中:
Bundle bundle =getIntent().getBundle();
mSelectedContacts = bundle.getParcelableArrayList("selectedContacts");
mContactListActivity = bundle.getBoolean("contactListActivity");