2016-05-13 15 views
0

Ich möchte Array-Liste von Objekt von einer Aktivität zu einer anderen senden. Dafür habe ich meine Klasse um Parcelable erweitert.Wie übergeben Sie ArrayList des Objekts zwischen Aktivitäten mit Parcelable?

Noch bekomme ich nicht die Liste. Auch ich habe eine boolesche Variable für die auch Wert Dosis nicht bekommen Änderung gesendet.

Meine Objektklasse:

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

} 

Erste Aktivität:

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(); 


     } 
    }); 

} 

Zweite Aktivität:

@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:

schon versucht, diese, wie durch sush vorgeschlagen:

Intent i = getIntent(); 

      Bundle bundle = i.getExtras(); 

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

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

Es wird dann eine Nullzeigerausnahme in der Liste ausgelöst.

Was läuft falsch?

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(); 

Zweite Aktivität:

Intent i = getIntent(); 


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

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

Jetzt habe ich dies getan, jetzt Boolean wird bestanden, aber die Liste zeigt Null.

Antwort

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); 

für Ihren Code

as per your code you should go like this 

    Bundle b = i.getExtras(); 
+0

ich schon versucht, diese jetzt ein Es wirft Nullzeigerausnahme für das Listenobjekt mSelectedcontacts. @sush –

+0

Bitte überprüfen Sie mein Update – Sush

0

Versuchen wie diese,

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); 

und erhalten, wie dies auf der zweiten Aktivität des onActivityResult Methode

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

Ich verwende nicht ContactsActivity. @Abhishek Patel –

+0

Dies ist Demo-Code, den Sie gemäß Ihrer Anforderung ändern können. –

0

ist das Problem: Sie sind nicht bunble immer in der zweiten Aktivität, sind Sie ein neues Bündel initialisiert, so dass Sie null erhalten:

In zweiter Aktivität:

  Bundle bundle =getIntent().getBundle(); 

      mSelectedContacts = bundle.getParcelableArrayList("selectedContacts"); 

      mContactListActivity = bundle.getBoolean("contactListActivity"); 
+0

Ich habe das versucht, noch Dosis nicht funktionieren. @Android Geek –

+0

Ich versuchte mit Serialisierung zu. Kannst du mir bitte den richtigen Weg zeigen? @Android Geek –

+0

Ich folgte diesem Link für serializable http: // stackoverflow.com/questions/13601883/how-to-pass-arraylist-von-objekten-from-one-to-award-activity-using-intention-in-an .. –

Verwandte Themen