Wenn auf eine Zelle geklickt wird, wird das Objekt einem Array hinzugefügt. Dieses Array muss an eine andere Aktivität übergeben werden. Zu Testzwecken habe ich das Array übergeben, das nur ein Objekt, das DoctorObject, enthält. Dann bekomme ich in der nächsten Aktivität den Namen als String mit .getName() und zeige das als Toast an. Der Toast ist jedoch leer.Übergabe der Arraylist des benutzerdefinierten Objekts an eine andere Aktivität
Hier einige Auszüge aus dem Code
Objekt:
public class DoctorObject implements Parcelable {
private List<Object> mChildrenList;
public DoctorObject(String name) {
Docname = name;
}
public DoctorObject (Parcel in){
this.DocPic = in.readInt();
this.Docname= in.readString();
this.Docspecialty = in.readString();
this.DocLocation = in.readString();
}
String Docname = "";
public String getDocname() {
return Docname;
}
public void setDocname(String docname) {
Docname = docname;
}
public int getDocPic() {
return DocPic;
}
public void setDocPic(int docPic) {
DocPic = docPic;
}
public String getDocspecialty() {
return Docspecialty;
}
public void setDocspecialty(String docspecialty) {
Docspecialty = docspecialty;
}
public String getDocLocation() {
return DocLocation;
}
public void setDocLocation(String docLocation) {
DocLocation = docLocation;
}
int DocPic;
String Docspecialty = "";
String DocLocation = "";
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public DoctorObject createFromParcel(Parcel in) {
return new DoctorObject(in);
}
public DoctorObject[] newArray(int size) {
return new DoctorObject[size];
}
};
Aktivität:
ArrayList<DoctorObject> selectedItems = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_my_team);
context = this;
/* Regular Expand listview
*/
// get the listview
expListView = (ExpandableListView) findViewById(R.id.newTeamListView);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataDoc, listDataChildStaff);
// setting list adapter
expListView.setAdapter(listAdapter);
// expListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
DoctorObject currentDoc = listDataDoc.get(groupPosition);
StaffObject currentStaff = listDataChildStaff.get(listDataDoc.get(groupPosition)).get(childPosition);
selectedItems.add(currentDoc);
String toastname = currentDoc.getDocname();
String toastStaff = currentStaff.getStaffname();
Toast.makeText(getApplicationContext(),
toastStaff,
Toast.LENGTH_SHORT).show();
return true;
}
});
/* End of regular expand listview
*/
nextButton = (Button) findViewById(R.id.nextbuttonTeam);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(CreateMyTeamActivity.this, CreateReferralTeamActivity.class);
myIntent.putParcelableArrayListExtra("NAME", selectedItems);
//myIntent.putExtra("Selections",selectedItems);
CreateMyTeamActivity.this.startActivity(myIntent);
}
});
}
Nächste Aktivität:
public class CreateReferralTeamActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_referral_team);
Intent b = getIntent();
ArrayList<DoctorObject> list = (ArrayList<DoctorObject>) b.getSerializableExtra("NAME");
String lists ="";
for (int i=0; i < list.size(); i++){
lists = list.get(i).getDocname();
}
//TextView text = (TextView) findViewById(R.id.sampleText);
//text.setText(lists);
Toast.makeText(getApplicationContext(),
lists,
Toast.LENGTH_SHORT).show();
}
}
Wenn ich das ausführe, scheint es, dass das Array leer ist. Hier
ist ein animiertes gif:
Ich habe es gerade versucht und es funktioniert immer noch nicht. Ich bekomme immer noch einen leeren Toast. – Adilp
@Adilp: Verwenden Sie 'Log.i (" TAG "," Liste SIZE: "+ list.size())' und überprüfen Sie die Größe von ArrayList in CreateReferralTeamActivity Aktivität –
07-27 09: 05: 33.547 4876-4876/com. adilpatel.vitalengine I/TAG: Liste GRÖSSE: 1 – Adilp