0

I need to "transfer" an ArrayList of custom class from one entity to another. I know that I need to implement Parcelable interface.

This is my custom class.

public class cSportsList implements Parcelable {
    boolean checked;
    String name;

    cSportsList(boolean check, String name_) {
        checked = check;
        name = name_;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //Non posso passare un booleano e non posso fare il cast boolean -> int
        if (checked) {
            dest.writeInt(1);
        }
        else dest.writeInt(0);
        dest.writeString(name);
    }

    public static final Parcelable.Creator<cSportsList> CREATOR = new Parcelable.Creator<cSportsList>() {
        public cSportsList createFromParcel(Parcel in) {
            return new cSportsList(in);
        }

        public cSportsList[] newArray(int size) {
            return new cSportsList[size];
        }
    };

    private cSportsList(Parcel in) {
        if (in.readInt() == 1) {
            checked = true;
        }
        else {
            checked = false;
        }
        name = in.readString();


    }
}

And this is the code in entity "from"

//This is sportsMap: ArrayList<cSportsList> sportsMap = new ArrayList<cSportsList>();
Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", (ArrayList<? extends Parcelable>)  sportsMap);  //I have tried with ArrayList<cSportsList> too.
this.startActivity(intent);

And this is the code in entity "to"

final Intent srcIntent = getIntent();
ArrayList<cSportsList> sportsMap = srcIntent.getParcelableExtra("sportsMap");

The problem is: in entity "To" sportsMap is null. If I set "breakpoint" in "writeToParcel" and "cSportsList(Parcelable in)" functions I see that the code is executed for both functions.

Where is my error ?

Thanks. M.

ZioBudda
  • 948
  • 2
  • 12
  • 24

5 Answers5

1

While reading you need to use

srcIntent.getParcelableArrayListExtra("sportsMap");

To put into Intent use below code

intent.putParcelableArrayListExtra("sportsMap", sportsMap);

and to read it from intent, use

ArrayList<cSportsList> sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

Read similar solution at Pass ArrayList<? implements Parcelable> to Activity

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0

You should use :

ArrayList<cSportsList> sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

You can also use Serialization for the same.

public class cSportsList implements Serializable

for put:

intent.putExtra("LIST",list);

for get:

(ArrayList<>)intent.getSerializableExtra("LIST");

KKSINGLA
  • 1,284
  • 2
  • 10
  • 22
0

Use the below code

sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

RaghavPai
  • 652
  • 9
  • 14
0

Try passing Arraylist instead of casting it like this:

Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", sportsMap);
        startActivity(intent);

In WhatSportActivity,

final Intent srcIntent = getIntent();
ArrayList<cSportsList> sportsMap = srcIntent.getParcelableExtra("sportsMap");

In case you still have problems,please try implementing Parceable by using "Parceable"plugin as It reduces the error in using Parceable lists.

Android Geek
  • 8,956
  • 2
  • 21
  • 35