3
How to send an Arraylist of objects to an Activity on Android?
ex: I have an Arraylist of Books objects, I want when I click this object arrayList to be passed to another Activity..
3
How to send an Arraylist of objects to an Activity on Android?
ex: I have an Arraylist of Books objects, I want when I click this object arrayList to be passed to another Activity..
10
There are three simple ways to pass a ArrayList as a parameter for a Activity.
Static methods:
In the ActivityA
// Dados a serem passados
ArrayList<Tipo> dados = ...;
Intent i = new Intent(this, ActivityB.class);
// Seta num campo estático da ActivityB
ActivityB.dados = dados;
startActivity(i);
In the ActivityB
@Override
public void onCreate(Bundle savedInstanceState) {
// Recupera os parâmetros passados pelo atributo estatico
ArrayList<Tipo> dados = ActivityB.dados;
// Limpar para nao ocorrer Leak
ActivityB.dados = null;
}
This shape is not good, because it forces a Coupling among the Activities, which is not a good practice, but it exists.
Using the interface java.lang.Serializable:
In the ActivityA
// Dados a serem passados
ArrayList<Tipo> dados = ...;
Intent i = new Intent(this, ActivityB.class);
// Seta num campo estático da ActivityB
i.putSerializableExtra("dados", dados);
startActivity(i);
Type Class
public class Tipo implements Serializable {
// Declaracao da sua classe
}
In the ActivityB
@Override
public void onCreate(Bundle savedInstanceState) {
// Recupera os parâmetros passados pelo atributo estatico
ArrayList<Tipo> dados = getIntent().getSerializableExtra("dados");
}
The class ArrayList implements the interface Serializable but, to work, the item that parameterizes the ArrayList also need to implement Serializable.
Using the interface Parcelable:
In the ActivityA
// Dados a serem passados
ArrayList<Tipo> dados = ...;
Intent i = new Intent(this, ActivityB.class);
// Seta num campo estático da ActivityB
i.putParcelableArrayListExtra("dados", dados);
startActivity(i);
Type Class
public class Tipo implements Parcelable {
// Declaracao da sua classe
public Tipo(Parcel in) {
// Esse construtor apesar de nao ser requerido pela interface,
// e necessario por causa do protocolo implicito do Parcelable
// Lembrar da ordem que foi escrita no writeToParcel!!
seuCampoInt = in.readInt();
seuCampoString = in.readString();
// Demais campos
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// Serializa os campos da sua classe,
// lembrando que essa ordem e importante no construtor
dest.writeInt(...);
dest.writeString(...);
}
// Como parte do contrato implicito,
// sua classe precisa de um atributo estatico chamado "CREATOR"
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Tipo createFromParcel(Parcel in) {
return new Tipo(in);
}
public Tipo[] newArray(int size) {
return new Tipo[size];
}
};
}
In the ActivityB
@Override
public void onCreate(Bundle savedInstanceState) {
// Recupera os parâmetros passados pelo atributo estatico
ArrayList<Tipo> dados = getIntent().getParcelableArrayListExtra("dados");
}
In my opinion, the best is using the interface Parcelable, proven to be faster1.
Any doubt about the Parcelable, take a look at the documentation of Parcelable and Parcelable.Creator and Parcel.
References:
Worked perfectly
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
You can go through extra on
Intent, provided that the items ofArrayListareParcelableorSerializable.– Wakim
Hi Felipe! Could you clarify a little more the question? Send from where? :)
– carlosrafaelgn
send from one Activity to another.. I edited
– felipe.rce
@Wakim I researched Parcelable, but I’m getting nothing, is there any way to answer the question with an example? I don’t understand how you implement Parcelable
– felipe.rce