0

I am trying to send an array list of objects in another activity. I have checked many articles on this topic and i am using parcelable but i am stuck at point where i cannot send the object.I have tried many things.This is the thing i am trying.

public class ParcalableForm implements Parcelable {

private ArrayList<form> from;


public ParcalableForm (ArrayList<form> choices) {
    this.from = choices;
}

public ParcalableForm (Parcel parcel) {
    this.from = parcel.readArrayList(null);
}



@Override
public int describeContents() {
    return 0;
}

// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(from);
}

// Method to recreate a Question from a Parcel
public static Creator<ParcalableForm> CREATOR = new Creator<ParcalableForm>() {

    @Override
    public ParcalableForm createFromParcel(Parcel source) {
        return new ParcalableForm(source);
    }

    @Override
    public ParcalableForm[] newArray(int size) {
        return new ParcalableForm[size];
    }

};

}

This is the parcelable class that implements Parcelable.I am trying to send Arraylist of form to another activity.

  Intent i = new Intent(UserPage.this,Form.class);

  Bundle extras = new Bundle();
  System.out.println("I found a form :- ");
  ParcalableForm p=new ParcalableForm(f1.attr);
  i.putExtra("geopoints", p);
  startActivity(i);

This is the class which is sending the object to the other activity.

Bundle extras = getIntent().getExtras();
ParcalableForm po =  new ParcalableForm(extras.getParcelableArrayList("geopoints"));

This is the part where i don't know how to get the object/Arraylist.I have tried many methods but no luck.Any ideas?

zeeshan dar
  • 89
  • 2
  • 10

2 Answers2

0

Pass Data to anther activity

 ArrayList<Animal> animals = new ArrayList<Animal>();
//fill your list with animals here

i.putExtra("animals", animals);

Receiving this data

ArrayList<Animal> animals = (ArrayList<Animal>) getIntent()
                        .getSerializableExtra("animals");
0

If what you want is to pass ArrayList<Form>, then use getParcelableArrayListExtra for this.
Generally, follow these steps :

  • Make your Form class properly implement Parcelable
  • In activity sending intent :

    // ArrayList<Form> myList - data to send; intent.putParcelableArrayListExtra("geopoints", myList);

  • In receiving activity :

    ArrayList<Form> myReceivedList = getIntent().getParcelableArrayListExtra("geopoints");

And don't forget null/sanity checks.

Hope that helps

Community
  • 1
  • 1
kiruwka
  • 9,250
  • 4
  • 30
  • 41