0

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;
}
Patil
  • 5
  • 4
  • what will be max data u passing via intent? – KOTIOS Sep 10 '14 at 06:02
  • Check [this](http://stackoverflow.com/questions/15747727/pass-arraylist-of-user-defined-objects-to-intent-android) – Kunu Sep 10 '14 at 06:11
  • Multiple ways to send data .. 1. Serializable 2.Parcelable 3.Make the array static and get it in the other activity(but it is against OOP) 4.Broadcast it and recieve it any where – Zar E Ahmer Sep 10 '14 at 06:19
  • Use the simplest way http://stackoverflow.com/questions/23003867/android-passing-an-object-to-another-activity/23004375#23004375 – Zar E Ahmer Sep 10 '14 at 06:23

5 Answers5

1

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...

Sreedev
  • 6,563
  • 5
  • 43
  • 66
  • What is PAR_KEY and where do you declare the object mCar .. can you explain – Zar E Ahmer Sep 10 '14 at 06:22
  • PAR_KEY is a key in the key value pair...You can use it as what ever you want. mObjCar you should decare it in the oncreate of the second activity where you want to get the intent.. – Sreedev Sep 10 '14 at 06:33
0

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.

Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27
0

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.

Farai
  • 180
  • 2
  • 12
0

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.

Sonali8890
  • 2,005
  • 12
  • 16
  • hello I'm new to Android and Already post my code yesterday but no one reply here is the details.. http://stackoverflow.com/questions/25737668/how-to-pass-imagesview-and-textview-to-next-listview-activity – Patil Sep 10 '14 at 06:49
  • now i add this arraylist in to that to pass to next activity that list View – Patil Sep 10 '14 at 06:55
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 you need ImageModel class implements serializable for putSerializable

public class ImageModel implements Serializable{

           // Generate getter and setter
    }
Htut
  • 267
  • 3
  • 18
  • here is the details.. http://stackoverflow.com/questions/25737668/how-to-pass-imagesview-and-textview-to-next-listview-activity – Patil Sep 10 '14 at 06:50
  • no i don't look from you refrence link. I'm answer your question that is from my experience men. – Htut Sep 10 '14 at 06:53