2

in an activity from my app I have an Intent to start a new Activity and I want to pass an ArrayList i.e. an ArrayList where each item is an instance of a own class ... how can I make this?

I have been seeing possible getExtras() like getStringArrayList() or getParcelableArrayList() and it doesn't work, I haven't found any valid type.

Can anyone help me? Thanks.

This is my class:

public class ItemFile {
  protected long id;
  protected String nombre;
  protected String rutaImagen;
  protected boolean checked;

  public ItemFile(long id, String nombre, String rutaImagen, boolean check) {
      this.id = id;
      this.nombre = nombre;
      this.rutaImagen = rutaImagen;
      this.checked = check;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getNombre() {
    return nombre;
  }

  public void setNombre(String nombre) {
    this.nombre = nombre;
  }

  public String getRutaImagen() {
      return rutaImagen;
  }

  public void setRutaImagen(String rutaImagen) {
      this.rutaImagen = rutaImagen;
  }

  public boolean isChecked() {
      return checked;
  }

  public void setChecked(boolean checked){
      this.checked = checked;
  }

}

How I must change it to set Parcelable?

KryNaC
  • 379
  • 4
  • 21
  • 1
    You can try by implementing MyClass with parcable. Take a look here. http://stackoverflow.com/a/7181792/1602333 – MohK Nov 06 '14 at 12:36
  • This is the best answer. In this post it is explained all about my question. The last answer into this post is very useful. – KryNaC Nov 06 '14 at 19:28

4 Answers4

4

First make sure MyClass class implements Parcelable interface then use this code for getting ArrayList in other Activity :

In your first activity do something like this:

ArrayList<MyClass> myClassList= new ArrayList<MyClass>();
Intent intent = new Intent(<YOUR ACTIVITY CONTEX>, <NEXT ACTIVITY>);
intent.putExtra("myClassList", myClassList);
startActivity(intent);

In your next activity do something like this:

ArrayList<MyClass> list = (ArrayList<MyClass>)getIntent().getExtras()getSerializable("myClassList");
Tushski
  • 242
  • 2
  • 12
1

Make MyClass Parcelable. See example below.

public class MyClass implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

 // Send it using
 ArrayList<MyClass> list;
 intent.putParcelableArrayListExtra("list", list);
Chirag Jain
  • 1,612
  • 13
  • 20
0

In First activity

ArrayList<MyClass> fileList = new ArrayList<MyClass>();
Intent intent = new Intent(MainActivity.this, secondActivity.class);
intent.putExtra("FILES_TO_SEND", fileList);
startActivity(intent);

In Secand

activity:ArrayList<MyClass> filelist =(ArrayList<MyClass>)getIntent().getSerializableExtra("FILES_TO_SEND");
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

I had been struggling with this myself but I found a fairly simple solution on tutorialspoint that worked well for me.

In the source activity, use

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("key", numbers);
            startActivity(intent);

Then in the destination activity, use

ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");

A link to the full tutorial is: https://www.tutorialspoint.com/how-to-pass-an-arraylist-to-another-activity-using-intents-in-android

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mich
  • 16
  • 1