Home » Android » android – List<String> readStringArray in Parcelable

android – List<String> readStringArray in Parcelable

Posted by: admin May 14, 2020 Leave a comment

Questions:

Most of my Parcelable is working;
Simple things like out.writeString, out.writeInt, in.readString() etc are working perfectly.

My problem is when I want to Parcel an Array / List / ArrayList (I’ve tried them all).

I’m currently trying with:

List<String>

and

out.writeStringList()

works fine.

Eclipse suggests that there is a

in.readStringList(List<String> list)

to read that data back in. But it’s not doing it for me.

What are we supposed to put into the ()?

I’ve tried nothing, with the result ‘Add argument to match…’
I’ve tried null, reference to the getter amongst others; which all return the error ‘cannot convert from void to List’

How to&Answers:

‘cannot convert from void to List’

From that error it sounds like you’re trying to assign the return type of the method readStringList(...) to a List<T> variable. In other words: are you perhaps writing something like:

List<String> stringList = in.readStringList(stringList)

? readStringList(...) returns a void, so that might be what Eclipse is complaining about. You actually should not be trying to a get a return type from a void method – in this case you need to supply the variable to which the result should be assigned as a parameter. Hence, this should suffice:

List<String> stringList = null;
in.readStringList(stringList)

By the way, if you’re implementing Parcelable in order to be able to pass relatively simple data objects between activities (using Intents), consider using the Serializable interface in stead – it’ll save you tons of work, especially if you need to repeat the same process for multiple objects.

Answer:

Write to parcel:

out.writeStringList(yourStringList);

Read from parcel

List<String> newList = in.createStringArrayList();