如何在Android中保存数据结构? [英] How do I save data structures in android?

查看:99
本文介绍了如何在Android中保存数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这样的结构存储一些数据:

I want to store some data from structures like this:

class Project {
    ChildA a;
    ChildB b;
}

class ChildA {
    ChildC c;
    ...
}
...

我有创建数据,如:

Projet x = new Project();
x.a = new ChildA();
x.a.c = new ChildC();

...我想将其保存到设备的SD卡上的外部文件,以便其他应用程序可以读取它(用户可以打开/复制它)。

... and I want to save it to an external file on the sdcard of my device so that other apps can read it (and users can open/copy it).

我以为我应该使用一个DOM解析器,因为我的数据结构不是很大,但我发现没有教程或任何类似的。有没有更好的方式存储这些信息?

I thought I should use a DOM parser because my data structures are not very big, but I find no tutorials or anything like that. Is there a better way to store this information?

如果没有,有什么说明如何使用android中的解析器?

If not, are there any instructions how to use the parsers in android?

推荐答案

如果您希望其他Android应用程序可以访问您的数据,那么推荐的方法是写一个内容提供者。另一方面,如果您希望将数据用于非Android应用程序,则需要导出。你必须自己编码。

If you want your data to be accessible to other Android applications, then the recommended way is to write a Content Provider. If, on the other hand you want your data to be used in non-android applications, you will need to "export" it. This you have to code on your own.

我现在可以想到三个选项:

I can think of three options right now:


  1. 创建XML文件

    不幸的是,Android不包括为方便地创建XML
    文件所必需的一切。最值得注意的是, javax.xml.transform 不可用,并且不推荐添加平台
    类。这意味着你必须在自己的
    转换类上写出它

  2. 创建JSON文件

    JSON是得到Android API的良好支持。请参阅此处这里

  3. 创建Java序列化文件

    只要您的对象实现了Serializable接口,那么可以轻松地将
    写入文件。我从来没有需要这样做,但它应该是
    直接,网络上应该可以使用示例。 (例如,Google通过这个迷你教程

  1. Create XML files
    Unfortunately, Android does not include all that is necessary to conveniently create XML files. Most notably, the javax.xml.transform is not available, and adding platform classes is not recommened. That means you will have to write it out on your own "transform" class
  2. Create JSON files
    JSON is well supported by the Android API. See here and here
  3. Create Java serialized files
    As long as your objects implement the interface "Serializable", then they can easily be written out to files. I've never needed to do that myself, but it should be straightforward, and examples should be available on the net. (As an example, Google came up with this mini-tutorial)

当您私有存储文件时,请考虑使用 openFileInput openFileOutput 。如果要明确存储在SD卡上,您应该使用 getExternalStorageDirectory()来检索根文件夹。

When you store your files privately, then consider using openFileInput and openFileOutput. If you want to store explicitly on the SD-Card, you should use getExternalStorageDirectory() to retrieve the root folder.

我个人的建议是使用JSON文件。这是一种简单的格式,在大多数情况下都很好,广泛可用。除了这个去/序列化是简单的。

My personal recommendation would be to use JSON files. It's a simple format which does well for most cases and it's widely available. Apart from that de/serialization is dead-simple.

这篇关于如何在Android中保存数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆