Vaibs_Cool
Vaibs_Cool

Reputation: 6160

How to pass List<custom object> through activities

ArrayList<Item> arrayOfList;

I want to pass arrayOfList; to next activity on Itemclick of listview

Tried Things

    Intent sec = new Intent(this, IndividualPage.class);
    Bundle b = new Bundle();
    b.putParcelableArrayList("mylist", arrayOfList);
    sec.putExtras(b);

To retrieve the arraylist

        Bundle b = this.getIntent().getExtras();
        ArrayList<Item> cats = b.getParcelableArrayList("mylist");
        System.out.println(cats);

But i am getting null in console.

Is there any other efficient way to pass the data.

Item.java

public class Item implements Parcelable {

private String Name;
private String Location;
private String Image;
private String Sector;
private int Founded;
private String Status;
private int RowVAls;



public String getName() {
    return Name;
}

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

public String getLocation() {
    return Location;
}

public void setLocation(String Location) {
    this.Location = Location;
}

public String getImage() {
    //return "http://23.253.164.20:8099/"+Image;
    return "http://23.253.164.20:8099/"+Image;
}

public void setImage(String Image) {
    this.Image = Image;
}

public String getSector() {
    return Sector;
}

public void setSector(String Sector) {
    this.Sector = Sector;
}

public int getFounded() {
    return Founded;
}

public void setFounded(int Founded) {
    this.Founded = Founded;
}

public String getStatus() {
    return Status;
}

public void setStatus(String Status) {
    this.Status = Status;
}

public int getRowVAls() {
    return RowVAls;
}

public void setRowVAls(int RowVAls) {
    this.RowVAls = RowVAls;
}

protected Item(Parcel in) {
        Name = in.readString();
        Location = in.readString();
        Image = in.readString();
        Sector = in.readString();
        Founded = in.readInt();
        Status = in.readString();
        RowVAls = in.readInt();
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() {
        @Override
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }

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


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(Name);
    dest.writeString(Location);
    dest.writeString(Image);
    dest.writeString(Sector);
    dest.writeInt(Founded);
    dest.writeString(Status);
    dest.writeInt(RowVAls);
}

Note:- Item is neither Parcelable nor Serializable . And i would not like to make any changes in that.

Upvotes: 0

Views: 379

Answers (3)

Graeme
Graeme

Reputation: 25864

As others have said, you can't do it properly without implementing parcelable - which sometimes isn't possible.

You're only other option is to architect a way in which you can put your list in a static context so it's accessible from wherever.

You could create a singleton SparseArray and related integer, and pass around the integer between activities describing the position in the SparseArray. You'd just have to ensure that you incremented your integer and removed the item from the SparseArray once you'd grabbed it. Off the top of my head -

private class ComplexObjectPassing {
    private static SparseArray<Object> sArray = new SparseArray<Object>();
    private static int count = 0;

    public static int putObject(Object obj) {
        count++;
        sArray.put(count, obj);
    }

    public static Object getObject(int index) {
        Object obj = sArray.get(index);
        sArray.put(index, null);
        return obj;
    }
}

Obviously, this would end with a huge memory leak when you passed things and they weren't pulled out by the new activity. All of the alternatives are going to have downsides and need to be carefully managed to reduce inevitable problems.

Upvotes: 0

Renjith
Renjith

Reputation: 3274

You should make your Item implement Parcelable. Try this site to make your Item Parcelable

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157487

Item is neither Parcelable nor Serializable . And i would not like to make any changes in that.

then you can't. I would strongly recommend you to look into the Parcelable interface, avoiding tricks like making the field public static

Upvotes: 1

Related Questions