Reputation: 5
I'm working on one assignment in that I want to pass Array List to Next activity using Intent. I want to display the data in list View for that I created one adapter also,but the problem is how to pass array list in intent.
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;
}
Upvotes: 0
Views: 981
Reputation: 265
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 you need ImageModel class implements serializable for putSerializable
public class ImageModel implements Serializable{
// Generate getter and setter
}
Upvotes: 0
Reputation: 6653
You can pass the arraylist using the parcelabale. You can pass any object through parcelable. Wrap your arraylist in parcelable and pass it as extras and in other activity you can get the parcelable and get the values. You can do it in three steps..Parcelable is more good than serializable due to many reasons...for knowing that chack this link out
1.Sample Parcelable class for the Arraylist
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.Getting parcelable from object in second activity
SampleParcelable mObjCar =(SampleParcelable)getIntent().getParcelableExtra(FirstActivity.PAR_KEY);
This code in github will help you more...
Upvotes: 1
Reputation: 2005
Please google before posting any question. Anyway try below code
ArrayList<ImageModel> modelList= new ArrayList<ImageModel>();
Bundle bundle = new Bundle();
bundle.putSerializable("array_list", modelList);
intent.putExtras(bundle);
For above code your ImageModel class should be serialized.
Upvotes: 0
Reputation: 190
either make your arraylist serializable serializable so buy making its contents serializable, by so doing you can send your arraylist via a bundle to the next intent.
Upvotes: 0
Reputation: 2085
Make your ImageModel class implement the Serializable interface. Then pass the arraylist as an extra:
Intent intent = new Intent(getApplicationContext(), MyActivityClass.class);
intent.putExtra("list", results);
startActivity(intent);
Then in your Activity's onCreate() you can retrieve the data:
ArrayList<ImageModel> list = (ArrayList<ImageModel>)getIntent().getSerializableExtra("list");
Hope it helps.
Upvotes: 0