将自定义对象的数组列表传递给另一个活动 [英] Passing arraylist of custom object to another activity

查看:80
本文介绍了将自定义对象的数组列表传递给另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击单元格时,对象将添加到数组中。该数组需要传递到另一个活动中。为了进行测试,我传递了只有一个对象DoctorDoc的数组。然后在下一个活动中,我使用.getName()作为字符串获取名称,并将其显示在烤面包中。但是,吐司是空的。

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.

下面是一些代码片段

对象:

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

活动:

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


        }
    });


}

下一个活动:

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.

以下是动画gif:

http://imgur.com/a/EGgL6

推荐答案

DoctorObject#writeToParcel()不能为空,这实际上是复制对象数据的内容。

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

要在其他活动中读回数据,请使用 getParcelableArrayListExtra

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

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

这篇关于将自定义对象的数组列表传递给另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆