Passing a List from one Activity to another
You can pass an ArrayList<E>
the same way, if the E
type is Serializable
.
You would call the putExtra (String name, Serializable value)
of Intent
to store, and getSerializableExtra (String name)
for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).
How to send a list of objects from one activity to another in android?
Try this
Code from Calling activity
Intent intent=new Intent(getApplicationContext(),NextActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("VAR1", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
Code on called activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<contact> arraylist = bundle.getParcelableArrayList("VAR1");
}
Pass list of objects from one activity to other activity in android
Use parcelable. Here is how you will do it:
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];
}
};
}
Passing the data:
Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", sharedBookingObject);
intent.putExtras(bundle);
startActivity(intent);
Retrieving the data:
Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");
How to pass object list from one activity to another in android?
You can send arralist of objects as below
intent.putParcelableArrayListExtra("some_key", (ArrayList<TYPE>) list);
startActivity(intent);
Retrieve it
ArrayList<TYPE> list = (ArrayList<TYPE>)getIntent().getParcelableArrayListExtra("some_key");
How to pass ArrayListCustomeObject from one activity to another?
You can pass an ArrayList<E>
the same way, if the E
type is Serializable
.
You would call the putExtra (String name, Serializable value)
of Intent
to store, and getSerializableExtra (String name)
for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Which way is better for pass a large list of data from one activity to another activity - android?
Best way to pass large data list from one Activity to another in Android is Parcelable . You first create Parcelable pojo class and then create Array-List and pass into bundle like key and value pair and then pass bundle into intent extras.
Below I put one sample User Pojo class that implements Parcelable interface.
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by CHETAN JOSHI on 2/1/2017.
*/
public class User implements Parcelable {
private String city;
private String name;
private int age;
public User(String city, String name, int age) {
super();
this.city = city;
this.name = name;
this.age = age;
}
public User(){
super();
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@SuppressWarnings("unused")
public User(Parcel in) {
this();
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
this.city = in.readString();
this.name = in.readString();
this.age = in.readInt();
}
public int describeContents() {
return 0;
}
public final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(city);
dest.writeString(name);
dest.writeInt(age);
}
}
ArrayList<User> info = new ArrayList<User>();
info .add(new User("kolkata","Jhon",25));
info .add(new User("newyork","smith",26));
info .add(new User("london","kavin",25));
info .add(new User("toranto","meriyan",30));
Intent intent = new Intent(MyActivity.this,NextActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("user_list",info );
intent.putExtras(bundle);`
startActivity(intent );
How to Send list of data from one activity to another one activity in android
Finally i completed using the parcelable in my class
public class Channel implements Serializable, Parcelable {
/** */
private static final long serialVersionUID = 4861597073026532544L;
private String cid;
private String uniqueID;
private String name;
private String logo;
private String thumb;
/**
* @return the cid
*/
public String getCid() {
return cid;
}
/**
* @param cid
* the cid to set
*/
public void setCid(String cid) {
this.cid = cid;
}
/**
* @return the uniqueID
*/
public String getUniqueID() {
return uniqueID;
}
/**
* @param uniqueID
* the uniqueID to set
*/
public void setUniqueID(String uniqueID) {
this.uniqueID = uniqueID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the logo
*/
public String getLogo() {
return logo;
}
/**
* @param logo
* the logo to set
*/
public void setLogo(String logo) {
this.logo = logo;
}
/**
* @return the thumb
*/
public String getThumb() {
return thumb;
}
/**
* @param thumb
* the thumb to set
*/
public void setThumb(String thumb) {
this.thumb = thumb;
}
public Channel(Parcel in) {
super();
readFromParcel(in);
}
public static final Parcelable.Creator<Channel> CREATOR = new Parcelable.Creator<Channel>() {
public Channel createFromParcel(Parcel in) {
return new Channel(in);
}
public Channel[] newArray(int size) {
return new Channel[size];
}
};
public void readFromParcel(Parcel in) {
String[] result = new String[23];
in.readStringArray(result);
this.cid = result[0];
this.uniqueID = result[1];
this.name = result[2];
this.logo = result[3];
this.thumb = result[4];
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.cid, this.uniqueID,
this.name, this.logo, this.thumb});
}}
In AcivityA use like this to send data.
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("channel",(ArrayList<Channel>) channels);
Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtras(bundle);
startActivity(intent);
In ActivityB use like this to get data.
Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");
How to pass a list of objects from one activity to another using parcelable
Check whether you have added RecyclerView in xml i.e, activity_second.xml
If you have added Recyclerview in xml check whether you have referenced it using findViewById in SecondActivity
RecyclerView CartItemsInRecyclerView = findViewById(R.id.recyclerview_id)
You are getting error for Layout Manager i.e referencing it using null object reference , that means CartItemsInRecyclerView is null
Edit :
In First activity:-
for (int i = 0; i < itemsInCart.size(); i++){
additems.add(itemsInCart.get(i));
}
//log statement
for (int i = 0; i < additems.size(); i++){
Log.d("firstActivity",i.getItenname())
}
In Second Activity:-
Instead of bundle.getgetParcelableArrayList try getIntent().getgetParcelableArrayList
ArrayList<Item> selecteditems =
getIntent().getParcelableArrayList("Itemselected");
//log statement
if(selecteditems.size()!=0){
for (int i = 0; i < selecteditems.size(); i++){
Log.d("secondActivity",i.getItenname())
}
}else{
Log.d("secondActivity","empty data")
}
Then check the result in Logcat
How to Pass a List of Objects from activity A to activity B in Kotlin?
In your activity B to get your list try like the following
var arr = this.getIntent().getParcelableArrayListExtra<Parcelable>("Personas")
and make sure your Persona
is extend Parcelable
like below.
class Persona() : Parcelable {
// ....
@Override
public void writeToParcel(Parcel dest, int flags) {
//....
}
private void readFromParcel(Parcel in) {
//....
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Creator<Persona> {
override fun createFromParcel(parcel: Parcel): Persona{
return Persona(parcel)
}
override fun newArray(size: Int): Array<Persona?> {
return arrayOfNulls(size)
}
}
}
Related Topics
Getting Started with Open Nfc Emulator
Enabling General JavaScript in Webviewclient
How to Print Stacktrace for an Exception Android
Determining If an Activity Exists on the Current Device
Using Enableautomanage() in Fragment
Android Httpclient Hangs on Second Request to the Server (Connection Timed Out)
Android Save Images to SQLite or Sdcard or Memory
Developing a Custom Lock Screen
After Upgrading to Google Play Services 9.0.0, App Hangs in Dynamitemodulesc
Android: Using Activitygroup to Embed Activities
Android - Inner Element Must Either Be a Resource Reference or Empty
How to Include Data Files with the App's APK
Android 1.5: Reading Sms Messages
Android: Getsupportactionbar() Always Returns Null in Actionbarsherlock Library
How to Avoid Delay in Android Gcm Messages/Change Heartbeat
Sms Delivery Report in Android