简体   繁体   中英

Can an Array be used instead of an arrayList for displaying items in recyclerView?

I am using sharedPreferences for storing certain information from the user. I am not using any sort of database at all, and I also dont want to use any unless necessary. My activity displays a recyclerView with 10-13 items (Dynamically added by user).

Currently I am using an arrayList to inflate data in the recyclerView . According to rules, It isn't possible to save an arrayList in sharedPreferenes (although there is a hack to do so, but its not recommended). So, instead, I want to save data in an array (two dimensional) and save it in sharedPreferences and inflate the recyclerView from the data in that array.

Is it even possible? Is it the right way for achieving what I want? I am new to this, please guide.

Is it even possible?

More or less, you can convert an array to an arraylist and then pass it to the recyclerview, but

Is it the right way for achieving what I want?

Not really, just use the datatype that fits the situation. If you need an arraylist for the recyclerview adapter, just use that.

there is a hack to do so, but its not recommended

I don't know what hack you speak of, or where you heard that it's not recommended, but serializing an ArrayList, even with custom objects, is very possible and this technique is widely used by android developers.

You should have a look at Serializable and Parcelable interfaces, they allow you to store an arraylist in sharedpreferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM