且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

保存 ArrayList 内容的***方法是什么?

更新时间:2022-12-18 08:54:24

您的问题类似于问我有这袋东西——我应该将这些东西转移到盒子、桶、桶还是垃圾箱?".

在不知道东西"是什么的情况下,我们无法轻易回答这个问题.

您的一般选择是使用数据库(也许实际上首先摆脱了 ArrayList)或持久化到文件(序列化、XML、JSON 等).>

一般来说,SQLite 数据库具有优势,即使用事务并且通常比仅编写自己的文件更健壮.但是,您的 ArrayList 可能存储的某些内容在数据库中可能无法正常工作.当然,您的 ArrayList 可能会存储一些甚至根本无法持久化的内容(例如,套接字、小部件).

I want to save an ArrayList so that it is persistent. The contents can change. What is the best way of approaching this in android?

Your question is akin to asking "I have this bag of stuff -- should I transfer the stuff to a box, bucket, barrel, or bin?".

Without knowing what the "stuff" is, we cannot easily answer the question.

Your general options are either to use a database (perhaps actually getting rid of the ArrayList in the first place) or persistence to a file (serialization, XML, JSON, etc.).

Generally speaking, a SQLite database has advantages, in that is uses transactions and will be generally more robust than just writing your own file. However, there are things your ArrayList might store that might not work well in a database. Of course, there are things your ArrayList might store that might not even be able to be persisted at all (e.g., a socket, a widget).