简体   繁体   English

可以使用Array而不是arrayList在recyclerView中显示项目吗?

[英]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. 我正在使用sharedPreferences来存储来自用户的某些信息。 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). 我的活动显示一个带有10-13项的recyclerView (由用户动态添加)。

Currently I am using an arrayList to inflate data in the recyclerView . 目前,我正在使用arrayList在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). 根据规则,不可能将arrayList保存在sharedPreferenes (尽管可以这样做,但是不建议这样做)。 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. 因此,我想将数据保存在一个数组(二维)中,并将其保存在sharedPreferences并从该数组中的数据中sharedPreferences recyclerView

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 或多或少,您可以将数组转换为arraylist,然后将其传递给recyclerview,但是

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. 如果您需要一个用于recyclerview适配器的arraylist,请使用它。

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. 我不知道您在说什么骇客,也不建议您在哪里听到,但是很有可能对ArrayList进行序列化(甚至使用自定义对象),并且该技术已被android开发人员广泛使用。

You should have a look at Serializable and Parcelable interfaces, they allow you to store an arraylist in sharedpreferences. 您应该看一下SerializableParcelable接口,它们允许您将数组列表存储在sharedpreferences中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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