Hey-men-whatsup Hey-men-whatsup - 2 months ago 24
Android Question

source.readList of Parcelable doesn't work over List<String>

This line is red flagged by IDE :

mBook.Color=source.readList(Color,Book.class.getClassLoader());


I also changed to

mBook.Color=source.readList(Color,String.class.getClassLoader());


but still wrong, I thought it's because my code was missing getter & setter for
color
field, but after creating them, it's still wrong. I'm sure this is not the way to make the field parceabled but don't know either the correct way to make correct

Book.java :

public class Book implements Parcelable {
private String bookName;
private int publishTime;
private static List<String> Color = Arrays.asList(new String[]{"red","blue"});

public String getBookName() {
return bookName;
}

public static List<String> getColor() {
return Color;
}

public static void setColor(List<String> Color) {
Book.Color = Color;
}

public void setBookName(String bookName) {
this.bookName = bookName;

}

public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}

public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.publishTime = source.readInt();
mBook.Color =source.readList(Color,Book.class.getClassLoader());
return mBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeInt(publishTime);
parcel.writeList(Color);
}
}

Answer

The problem is that readList returns a void so that might be what your IDE is complaining about. The solution is the following :

 mBook.Color = null;
 source.readList(Color,Book.class.getClassLoader());

Hope it helps.