Pasar lista de objetos de una actividad a otra actividad en android
Quiero pasar una lista de objetos de una actividad de otra actividad. Tengo una clase SharedBooking Abajo: –
public class SharedBooking { public int account_id; public Double betrag; public Double betrag_effected; public int taxType; public int tax; public String postingText; }
Código de la actividad de llamada: –
- El divisor Horizonal más alto y más inferior no aparece en ListView
- Cómo implementar un ListView con fastscroll y albhabet indexador
- Cómo manejar correctamente eventos de clic en Widget
- Cómo depurar con Android App Widget?
- AppWidgetHost no actualiza widgets
public List<SharedBooking> SharedBookingList = new ArrayList<SharedBooking>(); public void goDivision(Context context, Double betrag, List<SharedBooking> bookingList) { final Intent intent = new Intent(context, Division.class); intent.putExtra(Constants.BETRAG, betrag); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(intent); }
COde en la actividad llamada: –
Bundle extras = getIntent().getExtras(); if (extras != null) { amount=extras.getDouble(Constants.BETRAG,0); }
Cómo envío la lista de SharedBooking de una actividad, y recibo eso en la otra actividad.
Por favor, sugerirme cualquier enlace utilizable o código de ejemplo.
- ¿Se ejecuta un widget de Android en el mismo proceso que su aplicación?
- Cambiar la visibilidad del widget al hacer clic
- Problema con actividad cancelada por el sistema si ya no es visible en Android
- Si un texto en un TextView es más largo que el espacio disponible, ¿cómo obtener las líneas restantes?
- Crea una barra lateral como el navegador Dolphin o Firefox en android
- Widget de la pantalla de bloqueo de Android
- Android Spinner - ¿Cómo eliminar los botones de radio?
- Cómo cambiar el color del puntero de EditText (no el cursor)
Utilice parcelable . Aquí es cómo lo harás:
public class SharedBooking implements Parcelable{ public int account_id; public Double betrag; public Double betrag_effected; public int taxType; public int tax; public String postingText; public SharedBooking() { account_id = 0; betrag = 0.0; betrag_effected = 0.0; taxType = 0; tax = 0; postingText = ""; } public SharedBooking(Parcel in) { account_id = in.readInt(); betrag = in.readDouble(); betrag_effected = in.readDouble(); taxType = in.readInt(); tax = in.readInt(); postingText = in.readString(); } public int getAccount_id() { return account_id; } public void setAccount_id(int account_id) { this.account_id = account_id; } public Double getBetrag() { return betrag; } public void setBetrag(Double betrag) { this.betrag = betrag; } public Double getBetrag_effected() { return betrag_effected; } public void setBetrag_effected(Double betrag_effected) { this.betrag_effected = betrag_effected; } public int getTaxType() { return taxType; } public void setTaxType(int taxType) { this.taxType = taxType; } public int getTax() { return tax; } public void setTax(int tax) { this.tax = tax; } public String getPostingText() { return postingText; } public void setPostingText(String postingText) { this.postingText = postingText; } public int describeContents() { // TODO Auto-generated method stub return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(account_id); dest.writeDouble(betrag); dest.writeDouble(betrag_effected); dest.writeInt(taxType); dest.writeInt(tax); dest.writeString(postingText); } public static final Parcelable.Creator<SharedBooking> CREATOR = new Parcelable.Creator<SharedBooking>() { public SharedBooking createFromParcel(Parcel in) { return new SharedBooking(in); } public SharedBooking[] newArray(int size) { return new SharedBooking[size]; } }; }
Pasando los datos:
Intent intent = new Intent(getApplicationContext(),YourActivity.class); Bundle bundle = new Bundle(); bundle.putParcelable("data", sharedBookingObject); intent.putExtras(bundle); startActivity(intent);
Recuperación de los datos:
Bundle bundle = getIntent().getExtras(); sharedBookingObject = bundle.getParcelable("data");
Primero haga que la clase de la lista implemente Serializable.
public class Object implements Serializable{}
Entonces usted puede lanzar la lista a (Serializable). Al igual que:
List<Object> list = new ArrayList<Object>(); myIntent.putExtra("LIST", (Serializable) list);
Y para recuperar la lista que haces:
Intent i = getIntent(); list = (List<Object>) i.getSerializableExtra("LIST");
Eso es.
Clase de objeto parcelable
public class Student implements Parcelable { int id; String name; public Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int arg1) { // TODO Auto-generated method stub dest.writeInt(id); dest.writeString(name); } public Student(Parcel in) { id = in.readInt(); name = in.readString(); } public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { public Student createFromParcel(Parcel in) { return new Student(in); } public Student[] newArray(int size) { return new Student[size]; } }; }
Y la lista
ArrayList<Student> arraylist = new ArrayList<Student>();
Código de la actividad de llamada
Intent intent = new Intent(this, SecondActivity.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mylist", arraylist); intent.putExtras(bundle); this.startActivity(intent);
Código de la actividad llamada
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Bundle bundle = getIntent().getExtras(); ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist"); }
Es posible que desee implementar la Interfaz Parcelable en su clase SharedBooking y agregarlos al Intent, es decir, con el método putParcelableArrayListExtra . Consulte la documentación aquí: