我正在进行一项任务,因为我想使用Intent将Array List传递给Next活动。 我想在列表视图中显示数据,我也创建了一个适配器,但问题是如何在意图中传递数组列表。
private ArrayList<ImageModel> test(){
ArrayList results = new ArrayList();
ImageModel newsData = new ImageModel();
newsData.setQuestions("Q1.who belongs to melanistic color variant of any Panthera species");
newsData.setImageView(R.drawable.ic_blackpanther);
results.add(newsData);
newsData = new ImageModel();
newsData.setQuestions("Q2.who can run faster than any other land animal");
newsData.setImageView(R.drawable.ic_chita);
results.add(newsData);
newsData = new ImageModel();
newsData.setQuestions("Q3.who is belongs to family Elephantidae and the order Proboscidea");
newsData.setImageView(R.drawable.ic_elephant);
results.add(newsData);
newsData = new ImageModel();
newsData.setQuestions("Q4.Which animal is historically used in warfare");
newsData.setImageView(R.drawable.ic_horse);
results.add(newsData);
newsData = new ImageModel();
newsData.setQuestions("Q5.Which animal is united by their distinctive black and white stripes on his body");
newsData.setImageView(R.drawable.ic_zebra);
results.add(newsData);
return results;
}
答案 0 :(得分:1)
您可以使用parcelabale传递arraylist。您可以通过parcelable传递任何对象。将您的arraylist包裹在parcelable中并将其作为额外内容传递,在其他活动中,您可以获得parcelable并获取值。你可以分三步完成。由于很多原因,可以比可序列化更好...可以知道这个问题link
1.示例Arraylist的Parcelable类
public class SampleParcelable implements Parcelable {
String carName;
String modelNumber;
String carMake;
int carPrice;
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getModelNumber() {
return modelNumber;
}
public void setModelNumber(String modelNumber) {
this.modelNumber = modelNumber;
}
public String getmMake() {
return carMake;
}
public void setmMake(String mMake) {
this.carMake = mMake;
}
public int getPrice() {
return carPrice;
}
public void setPrice(int price) {
carPrice = price;
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<SampleParcelable> CREATOR = new Creator<SampleParcelable>() {
public SampleParcelable createFromParcel(Parcel source) {
SampleParcelable mBook = new SampleParcelable();
mBook.carName = source.readString();
mBook.carMake = source.readString();
mBook.modelNumber = source.readString();
mBook.carPrice = source.readInt();
return mBook;
}
public SampleParcelable[] newArray(int size) {
return new SampleParcelable[size];
}
};
@Override
public void writeToParcel(Parcel mParcel, int flags) {
mParcel.writeString(carName);
mParcel.writeString(modelNumber);
mParcel.writeString(carMake);
mParcel.writeInt(carPrice);
}
}
2.Bundling parcelable to a intent
Intent pIntent = new Intent(this, SecondActivity.class);
Bundle pBundle = new Bundle();
pBundle.putParcelable(PAR_KEY, mCar);
pIntent.putExtras(pBundle);
//starting next Activity with intent
startActivity(pIntent);
3.在第二项活动中获取可从对象获取的内容
SampleParcelable mObjCar =(SampleParcelable)getIntent().getParcelableExtra(FirstActivity.PAR_KEY);
github中的这个code将为您提供更多帮助......
答案 1 :(得分:0)
让 ImageModel 类实现 Serializable 界面。然后传递arraylist作为额外的:
Intent intent = new Intent(getApplicationContext(), MyActivityClass.class);
intent.putExtra("list", results);
startActivity(intent);
然后在您的Activity的onCreate()中,您可以检索数据:
ArrayList<ImageModel> list = (ArrayList<ImageModel>)getIntent().getSerializableExtra("list");
希望它有所帮助。
答案 2 :(得分:0)
要么使你的arraylist可序列化serializable所以购买使其内容可序列化,这样你就可以通过捆绑发送你的arraylist到下一个意图。
答案 3 :(得分:0)
在发布任何问题之前请先google。 无论如何尝试下面的代码
ArrayList<ImageModel> modelList= new ArrayList<ImageModel>();
Bundle bundle = new Bundle();
bundle.putSerializable("array_list", modelList);
intent.putExtras(bundle);
对于上面的代码,您的ImageModel类应该被序列化。
答案 4 :(得分:0)
MainActivity.class
private ArrayList<ImageModel> listItem;
Bundle bundle = new Bundle();
bundle.putSerializable("array_list",listItem);
Intent intent = new Intent(getApplicationContext(), MyActivityClass.class);
intent.putExtra(intent);
startActivity(intent);
// and to another Activity get it as.
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
ArrayList<ImageModel> listItem=
(ArrayList<ImageModel>)bundle.getSerializable("array_list");
ImageModel.class 你需要ImageModel类为putSerializable实现serializable
public class ImageModel implements Serializable{
// Generate getter and setter
}