我试图通过使用Bundle将数组专家从主要活动转移到另一个活动。我创建了一个实现Parceable
的模型类。然后,我进入片段ArrayList<EssayElement> essayList =bundle.getParcelableArrayList("essayList");
。该捆绑软件具有值,但列表无法获取该值。
模型类
package com.example.myapplication;
import android.os.Parcel;
import android.os.Parcelable;
public class EssayElement implements Parcelable {
//Declare variables
public int id;
public String title;
public String essay;
public String date;
public String time;
public EssayElement() {
}
protected EssayElement(Parcel in) {
id = in.readInt();
title = in.readString();
essay = in.readString();
date = in.readString();
time = in.readString();
}
public static final Creator<EssayElement> CREATOR = new Creator<EssayElement>() {
@Override
public EssayElement createFromParcel(Parcel in) {
return new EssayElement(in);
}
@Override
public EssayElement[] newArray(int size) {
return new EssayElement[size];
}
};
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEssay() {
return essay;
}
public void setEssay(String essay) {
this.essay = essay;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getId() {
return id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(title);
parcel.writeString(essay);
parcel.writeString(date);
parcel.writeString(time);
}
}
散文活动
public class Essay extends AppCompatActivity {
//declare variables
TextView tv;
TextView tv1;
TextView tv2;
TextView tv3;
private ViewPager viewPager; //The pager widget, which handles animation and allows swiping horizontally to access previous and next wizard steps.
private FragmentAdapter fragmentAdapter; //The pager adapter, which provides the pages to the view pager widget.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
ArrayList<EssayElement> essayList = bundle.getParcelableArrayList("essayList");
setContentView(R.layout.essay);
viewPager = findViewById(R.id.pager);
tv = (TextView) findViewById(R.id.title);
tv1 = (TextView) findViewById(R.id.essay);
tv2 = (TextView) findViewById(R.id.date);
tv3 = (TextView) findViewById(R.id.time);
FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), essayList);
viewPager.setAdapter(fragmentAdapter);
答案 0 :(得分:0)
实现通过包将可序列化为模型类。
bundle.putSerializable("coupn_code",(Serializable) cartModelLists.get(i).getCouponModels());
像这样进入片段/活动,并将此列表传递给适配器。希望您能得到答案。
couponModels = (ArrayList<CouponModel>)getArguments().getSerializable("coupn_code");