简体   繁体   English

Android,存储数据列表的最佳方法

[英]Android, Best way to store lists of data

I'm stuck on how I should go about storing some user data in my application I'm building. 我被困在如何将一些用户数据存储在正在构建的应用程序中的问题上。

The data being stored is effectively a list of items, like a list of car objects. 实际上,存储的数据是项目列表,例如汽车对象列表。 The application must be able to store multiple lists. 该应用程序必须能够存储多个列表。

Now i know there's several ways to store data, but I'm unsure what would be the best approach to storing these multiple lists. 现在我知道有几种存储数据的方法,但是我不确定什么是存储这些多个列表的最佳方法。

so far i can only think of: 到目前为止,我只能想到:

1) create table(s) for the lists. 1)为列表创建表。

2) storing each list as either a text file or xml file. 2)将每个列表存储为文本文件或xml文件。

if anyone can explain what would be the best approach and if there are better alternatives what they would be. 如果有人可以解释什么是最好的方法,以及是否有更好的选择,他们会是什么。

There are basically a few options: 基本上有几种选择:

  1. Use a SQlite Database and optionally hide it behind a ContentProvider 使用SQlite数据库并将其隐藏在ContentProvider后面
  2. Let your Classes implement Serializable and save things with ObjectStream to some file to persist them 让您的类实现Serializable并将其与ObjectStream一起保存到某个文件中以持久化它们
  3. Implement some custom String serialization (eg. JSONObject / JSONArray / XML) to save your objects in SharedPreferences 实现一些自定义的字符串序列化(例如JSONObject / JSONArray / XML)以将您的对象保存在SharedPreferences中

Pros/Cons: 优点缺点:

  1. Quite an overhead but faster than the others when you got more than a couple of objects. 当您拥有多个对象时,开销很大,但比其他开销更快。
  2. Easy to implement, not that fast, OK for a couple objects 易于实现,不是那么快,对于几个对象来说还可以
  3. Easy to implement, even slower, OK for a few objects, upgrading your data structure can break thinks. 易于实现,甚至更慢,对于一些对象来说还可以,升级数据结构可能会破坏思维。 be prepared. 做好准备。

So in the end, it depends on your use case. 因此,最后取决于您的用例。

Further readings: https://developer.android.com/guide/topics/data/data-storage.html 进一步阅读: https : //developer.android.com/guide/topics/data/data-storage.html

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

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