sahaj jain
sahaj jain

Reputation: 27

How to send a list of objects from one activity to another in android?

I have created the following class and using objects of this class. I want to pass a list of this class and send it to the next activity.

class contact implements Comparable<contact>, Parcelable {
    String name;
    private String phone;
    private Bitmap image;

    contact(String n, String p, Bitmap pic) {
        super();
        name = n;
        phone = p;
        image = pic;
    }

    public contact(Parcel in) {
        String[] data = new String[3];
        in.readStringArray(data);
        this.name=data[0];
        this.phone=data[1];
        byte [] encodeByte= Base64.decode(data[2],Base64.DEFAULT);
        this.image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    Bitmap getImage() {
        return image;
    }

    public void setName(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    String getPhone() {
        return phone;
    }

    static Comparator<contact> ContactNameComparator
        = new Comparator<contact>() {
            public int compare(contact c1, contact c2) {
                String name1 = c1.getName().toUpperCase();
                String name2 = c2.getName().toUpperCase();

                return name1.compareTo(name2);
            }
    };

    @Override
    public int compareTo(@NonNull contact o) {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray(new String[]{this.getName(),this.getPhone(),String.valueOf(getImage())});
    }
    public static final Creator<contact> CREATOR = new Creator<contact>(){

        @Override
        public contact createFromParcel(Parcel in) {
            return new contact(in);
        }

        @Override
        public contact[] newArray(int size) {
            return new contact[size];
        }
    };
}

In my MainActivity, I am creating a list

List<contact> c=new ArrayList<>();

And after adding some objects to this list, I am trying to send it to the next activity though putExtra() but am getting an error. I am trying to pass the list like this,

Intent intent=new Intent(getApplicationContext(),NextActivity.class);
        intent.putExtra("VAR1", (Parcelable) c);
        startActivity(intent); 

And in my NextActivity.class, am receiving it like this,

    Bundle bundle =  getIntent().getExtras();
    c = bundle.getParcelableArrayList("VAR1" );

Upon running the app, I am getting the following error:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable

How should I solve this problem?

Upvotes: 0

Views: 4322

Answers (1)

akhilesh0707
akhilesh0707

Reputation: 6899

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");
}

Upvotes: 1

Related Questions