Adilp Adilp -5 years ago 117
Android Question

Passing arraylist of custom object to another activity

When a cell is clicked the object is added to an array. This array needs to be passed into another activity. For testing purposes I have passed the array that has only one object, the DoctorObject. Then in the next activity I get the name as a string using .getName() and display that in a toast. However the toast is empty.

Here is some snippets of the code


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 = "";

public int describeContents() {
return 0;

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


ArrayList<DoctorObject> selectedItems = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {

context = this;

/* Regular Expand listview


// get the listview
expListView = (ExpandableListView) findViewById(;

// preparing list data

listAdapter = new ExpandableListAdapter(this, listDataDoc, listDataChildStaff);

// setting list adapter

// expListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

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


String toastname = currentDoc.getDocname();
String toastStaff = currentStaff.getStaffname();


return true;

/* End of regular expand listview


nextButton = (Button) findViewById(;

nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(CreateMyTeamActivity.this, CreateReferralTeamActivity.class);
myIntent.putParcelableArrayListExtra("NAME", selectedItems);



Next Activity:

public class CreateReferralTeamActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

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





When I run this it seems like the array is empty.

Here is an animated gif:

Answer Source

You can't have DoctorObject#writeToParcel() empty, that is what actually copies the object data.

public void writeToParcel(Parcel dest, int flags) {
    // the order you writeto the Parcel has to be the same order you read from the Parcel
    dest.writeInt(DocPic );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download