将自定义对象的数组列表传递给另一个活动
问题描述:
单击单元格时,该对象将添加到数组中。这个数组需要被传递给另一个活动。出于测试目的,我已经传递了只有一个对象DoctorObject的数组。然后在下一个活动中,我使用.getName()作为字符串获取该名称,并将其显示在吐司中。然而,吐司是空的。将自定义对象的数组列表传递给另一个活动
下面是代码
对象的一些片段:
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();
}
}
当我运行它看起来像数组是空的。
这里是一个GIF动画:
答
你不能有DoctorObject#writeToParcel()
空的,是实际复制对象数据的内容。
@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
ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME");
答
使用getParcelableArrayListExtra
代替getSerializableExtra
,因为使用putParcelableArrayListExtra
距上一次活动发送ArrayList对象:
ArrayList<DoctorObject> list = b.getParcelableArrayListExtra("NAME");
答
您可以使用GSON图书馆传递一个ArrayList到另一个活动的情况下,如果T类型的对象不是Parcelable对象。通用类型在哪里。在你的情况下,ArrayList的
步骤1:首先导入GSON图书馆使用摇篮脚本
dependencies {
compile 'com.google.code.gson:gson:2.7'
}
第2步加入这一行的摇篮依赖性:转换的ArrayList成JSON字符串,并传递JSON字符串从活动1到活动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);
步骤3:从捆绑获取JSON字符串并将其转换为ArrayList的背面
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);
我刚试过,它仍然没有工作。我仍然得到一片空白的烤面包。 – Adilp
@Adilp:使用'Log.i(“TAG”,“list SIZE:”+ list.size())'并检查CreateReferralTeamActivity活动 –
07-27 09:05:33.547 4876-4876/com中ArrayList的大小。 adilpatel.vitalengine I/TAG:列表大小:1 – Adilp