Android Class Parcelable with ArrayList
If you need to pass an ArrayList
between activities, then I'd go with implementing Parcelable
also, as there is no other way around I guess.
However I don't think you will need that much of getters and setters. Here is your Question
class which implements Parcelable
:
public class Question implements Parcelable {
public String id;
public String text;
public String image;
public ArrayList<Choice> choices;
/**
* Constructs a Question from values
*/
public Question (String id, String text, String image, ArrayList<Choice> choices) {
this.id = id;
this.text = text;
this.image = image;
this.choices = choices;
}
/**
* Constructs a Question from a Parcel
* @param parcel Source Parcel
*/
public Question (Parcel parcel) {
this.id = parcel.readString();
this.text = parcel.readString();
this.image = parcel.readString();
this.choices = parcel.readArrayList(null);
}
@Override
public int describeContents() {
return 0;
}
// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(choices);
}
// Method to recreate a Question from a Parcel
public static Creator<Question> CREATOR = new Creator<Question>() {
@Override
public Question createFromParcel(Parcel source) {
return new Question(source);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
}
Making data class with ArrayList inside Parcelable
Use the following within your Constructor:
parcel.createStringArrayList()
and within writeToParcel
:
parcel.writeStringList(this.mTest)
Just a side node: If you're using Android Studio, just let it generate the Parcelable with the Parcelable code generator Plugin.
ArrayListArrayListString in Parcelable object kotlin
You can't directly create Parcelable
for List
of List
directly, so one solution is to make one subclass of your desired List
as Parcelable
and take it as you final list type. How? check out below :
Let's first create our internal List of String class like below :
class StringList() : ArrayList<String>(), Parcelable {
constructor(source: Parcel) : this() {
source.createStringArrayList()
}
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeStringList(this@StringList)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<StringList> = object : Parcelable.Creator<StringList> {
override fun createFromParcel(source: Parcel): StringList = StringList(source)
override fun newArray(size: Int): Array<StringList?> = arrayOfNulls(size)
}
}
}
What we've done here is created our ArrayList<String>
parcelable so that we can use it at any endpoint.
So final data class would be having following implementation :
data class Foo(@SerializedName("bar") val bar: List<StringList>) : Parcelable {
constructor(source: Parcel) : this(
source.createTypedArrayList(StringList.CREATOR)
)
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
writeTypedList(bar)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Foo> = object : Parcelable.Creator<Foo> {
override fun createFromParcel(source: Parcel): Foo = Foo(source)
override fun newArray(size: Int): Array<Foo?> = arrayOfNulls(size)
}
}
}
Note: It's simple implementation based on O.P., you can make any customization based on your requirement.
How to make generic arraylist parcelable in Android?
I have given up implementing AList
as a Parcelable
. However, since AList
is ArrayList
-like, I can treat it exactly as an ArrayList
when I need to write AList<T>
to Parcel.
For example, I have a AList<Tag> Tags
declared in Food
class. So in protected Food(Parcel in)
, I can write:
ArrayList<Tag> tags = new ArrayList<>();
in.readTypedList(tags, Tag.CREATOR);
Tags = new AList<>(tags);
Of course, I have implemented Tag
as a Parcelable
. And I can have such writeToParcel
:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeString(ImagePath);
dest.writeTypedList(Tags.ToArrayList());
dest.writeString(Note);
dest.writeByte((byte) (IsFavorite ? 1 : 0));
if (DateAdded == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeLong(DateAdded);
}
}
My source code for Food
class is here.
how to read Int arraylist in parcelable in android
So to create a parcelable class.
Firstly, Add add plugin to the gradle
plugins {
.
.
id 'kotlin-parcelize'
}
Then, create a class and use annotations to parcelize the data
@Parcelize
class PostCardsListData(
@SerializedName("arrDescriptionsTags")
var arrDescriptionsTags: ArrayList<String>? = null,
@SerializedName("priceDetail")
var priceDetail: ArrayList<Int?>? = null
) : Parcelable
To read/write data you just need to create an object of this class and use the dot operator to access the list.
In the activity/fragment/viewModel class
private var listData = PostCardsListData()
//read data
private var priceList : ArrayList<Int> = ArrayList()
if(listData.priceDetail != null){
priceList.addAll(listData.priceDetail)
}
how to properly implement Parcelable with an ArrayListParcelable?
You almost got it !
You just need to do :
public void writeToParcel(Parcel out, int flags) {
out.writeString(_mac);
out.writeString(_pan);
out.writeInt(_band);
out.writeSerializable(_lqis);
out.writeTypedList(_devices);
}
private ZigBeeNetwork(Parcel in) {
_mac = in.readString();
_pan = in.readString();
_band = in.readInt();
_lqis = (ArrayList<Integer>) in.readSerializable();
in.readTypedList(_devices, ZigBeeDev.CREATOR);
}
That's all!
For your list of Integer, you can also do :
out.writeList(_lqis);
_lqis = new ArrayList<>();
in.readList(_lqis Integer.class.getClassLoader());
It should work.
How to implement Parcelable on my class which contains an ArrayListString?
I'm using Java instead of Kotlin but I think this problem can handled by a same solution.
I often use:
- parcel.createStringArrayList() instead of parcel.readArrayList(null)
- parcel.writeStringList(transactionImages) instead of parcel.writeList(transactionImages)
Making ArrayList of custom objects parcelable
Use writeTypedList and readTypedList
.
parcel.writeTypedList(yourList);
and
parcel.readTypedList(yourList, YourParcelable.CREATOR);
Notice how you pass in the CREATOR as an argument for reading the list back.
Related Topics
Android:Change Button Text and Background Color
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
How to Dynamically Change an App Icon Like Calendar App Does
Get the Co-Ordinates of a Touch Event on Android
Proxy Which Requires Authentication with Android Emulator
Practical Way to Find Out If Sms Has Been Sent
Custom 'Keyboard' Built in an Application on Android
How Does One Implement Drag and Drop for Android Marker
Size of Android Notification Bar and Title Bar
Webview Rendering Issue in Android Kitkat
Mediametadataretriever.Getframeattime() Returns Only First Frame
Adding Multiple Markers in Google Maps API V2 Android
After Update of as to 1.0, Getting "Method Id Not in [0, 0Xffff]: 65536" Error in Project
Setsupportactionbar Toolbar Cannot Be Applied to (Android.Widget.Toolbar) Error