3

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

Object:

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

Activity:

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


        }
    });


}

Next Activity:

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

}

}

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

Here is an animated gif:

https://i.sstatic.net/qfxzT.jpg

3 Answers 3

8

You can use Gson Library to Pass an ArrayList to another Activity in case if you T object is not a Parcelable Object. Where is Generic Type. In your case, it is ArrayList

Step 1: First Import Gson Library using Gradle Script

Add this line in your Gradle dependency

dependencies {
    compile 'com.google.code.gson:gson:2.7'
}

Step 2: Convert ArrayList into JSON String and pass the JSON String from Activity 1 to Activity 2

        ArrayList<DoctorObject> doctors = new ArrayList<Location>();
        doctors.add(new DoctorObject(params));
        doctors.add(new DoctorObject(params));

        Gson gson = new Gson();
        String jsonString = gson.toJson(doctors);
        Intent intent = new Intent(MainActivity.this,MapsActivity.class);
        intent.putExtra("KEY",jsonString);
        startActivity(intent);

Step 3: Get the JSON String from Bundle and convert it to ArrayList back

    import java.lang.reflect.Type;


    Bundle bundle = getIntent().getExtras();
    String jsonString = bundle.getString("KEY");

    Gson gson = new Gson();
    Type listOfdoctorType = new TypeToken<List<DoctorObject>>() {}.getType();
    ArrayList<DoctorObject> doctors = gson.fromJson(jsonString,listOfdoctorType );
Sign up to request clarification or add additional context in comments.

Comments

1

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    // the order you write to the Parcel has to be the same order you read from the Parcel
    dest.writeInt(DocPic);
    dest.writeString(Docname);
    dest.writeString(Docspecialty);
    dest.writeString(DocLocation);
}

And to read back your data in the other activity, use getParcelableArrayListExtra

ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME");

Comments

1

Use getParcelableArrayListExtra instead of getSerializableExtra because sending ArrayList object from previous Activity using putParcelableArrayListExtra:

 ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME");

3 Comments

I just tried it and its still not working. I still get a blank toast.
@Adilp: Use Log.i("TAG","list SIZE :"+list.size()) and check size of ArrayList in CreateReferralTeamActivity Activity
07-27 09:05:33.547 4876-4876/com.adilpatel.vitalengine I/TAG: list SIZE :1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.