Question:
I have a recycleview with data and I need to pass all the items data to another activity, how can I implement this?
Answer:
Why make such a crutch as in the previous answer, if any model can be made serializable ( implements Parcelable
) and pass the entire model as a whole?
Put in intent: intent.putExtra("parcelable", myParcelables.get(position))
Take from intent: MyParcelable model = getIntent().getParcelableExtra("parcelable")
implementation example:
public class Country implements Parcelable {
private String code;
private String name;
private String tag;
private int limit;
private String image;
public Country(JSONParser data) {
code = data.getString("code");
name = data.getString("value");
tag = data.getString("flag");
image = data.getString("image");
if (data.contains("limit")) {
limit = data.getInt("limit") - code.length();
}
}
public Country(Parcel in) {
code = in.readString();
name = in.readString();
tag = in.readString();
image = in.readString();
limit = in.readInt();
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
@Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
@Override
public Country[] newArray(int size) {
return new Country[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(code);
dest.writeString(name);
dest.writeString(tag);
dest.writeString(image);
dest.writeInt(limit);
}
public String getClearCode() {
return code;
}
public String getCode() {
return "+" + code;
}
public String getName() {
return name;
}
public String getTag() {
return tag;
}
public int getLimit() {
return limit;
}
public String getImage() {
return image;
}
}
PS
In order not to catch BadParcelableException
when passing between activities, the Parcelable constructor must initialize the data in the same order in which they were written to Parcel
in the void writeToParcel(Parcel, int)
method