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

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

问题描述

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

I want to store some data from structures like this:

class Project {
    ChildA a;
    ChildB b;
}

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

我已经创建的数据,如:

I have created data like:

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?

如果不是,是否有任何说明如何使用解析器的Andr​​oid?

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

推荐答案

如果您希望自己的数据是其他Android应用程序访问,那么推荐的方法是写的内容提供商。如果,你想在非Android应用程序中使用您的数据。另一方面,则需要出口了。这你得code你自己了。

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中的不可用,并加入平台
    类不recommened。这意味着你必须把它写在你自己
    改造类


  2. 创建JSON文件

    JSON是深受Android的API支持。请参见这里和的这里

  3. 创建Java序列化的文件

    只要你的对象实现了接口序列化,那么他们可以很容易地
    写出到文件中。我从来没有需要做的是我自己,但它应该是
    简单,例子应该可以在网络上。 (作为一个例子,谷歌来
    这个迷你型的教程

  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)

当您存储文件私下里,那么可以考虑使用<一个href=\"http://developer.android.com/reference/android/content/Context.html#openFileInput%28java.lang.String%29\"相对=nofollow> openFileInput 和<一个href=\"http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String%2C%20int%29\"相对=nofollow> openFileOutput 。如果你想明确地存储在Micro-SD卡,你应该使用<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29\"相对=nofollow> 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天全站免登陆