简体   繁体   中英

Send an Arraylist of an object from one activity to another

I need to duplicate an ArrayList from one activity from another. This is an ArrayList of an object that I named Dias , and it contains a String and a boolean:

Arraylist {Dias}  // Dias contains(String Dias, boolean estado)

And I have to pass this ArrayList to other activity.

My Dias class:

public class Dias {

  private String Dia;
  private boolean estado;

//CONSTRUCTOR DE LA CLASE//
  public Dias(String Dia, boolean estado) {
      this.Dia = Dia;
      this.estado = estado;
  }

//GETTERS Y SETTERS DE LA CLASE//

  public String getDia() {
      return Dia;
  }

  public void setDia(String dia) {
      Dia = dia;
  }

  public boolean isChekeado() {
      return estado;
  }

  public void setChekeado(boolean chekeado) {
      estado = chekeado;
  }

}

My Primary Class:

 public class Primera extends Activity {
    ArrayList<Dias> dias = new ArrayList<Dias>();
     //OnClick Method
    public void lanzar2(View view){
      dias.add(new Dias("Lu", false));
      dias.add(new Dias("MAr", false));
      Intent i = new Intent();
      Bundle b = new Bundle();
      b.putParcelableArrayList("arreglo", (ArrayList<? extends Parcelable>) dias);
      i.putExtras(b);
      i.setClass(this, ListasActivity.class);
      startActivity(i);
   }

}

How can I send my Arraylist to another activity?, I just don't understand how does it work(parcelable) and also I don't know the syntax to use it.

Thank you!

You have to implement the interface Parcelable in the object Dias. You can see more about how to do it here: http://developer.android.com/reference/android/os/Parcelable.html

Besides that you would send through an Intent that you use to start the other activity the ArrayList using the method:

)">intent.putParcelableArrayListExtra(nameToSaveAs, yourList)

Then you startActivity(intent) and you can receive the data in the other activity with getIntent().getParcelableArrayListExtra(nameToSaveAs); You will probably have to cast it, but that is the basic steps to pass ArrayLists.

Another simple method is to serialize it in your own way.

For example :

If you have 5 elements in your arraylist like "hello", "how" , "are", "you", "brother" . You can loop through the arraylist and make a string as hello,how,are,you,brother

String serializedString = "";
for(String anElement:arrayList)
    serilaizedString = serializedString + "," +anElement;

and send it to another activity using putExtra method of Intent.

And in the receiving end, you can split the text using split(",") then you will get an array which you can change to array list again if needed.

String[] myArray = recievedString.split(",");
List<String> myList = new List<String>();
for(String anElement:myList)
    myList.add(anElement);

Now you have myList as the array list.

PS Your strings shouldn't contain any comma though. So for cases with comma, you can use something like $ or % or what ever is comfortable for case.

You can pass it either using Serialization or by using Parcelable interface. Refer to this for Parcelable interface http://developer.android.com/reference/android/os/Parcelable.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM