如何在Android中保存数据结构?

编程入门 行业动态 更新时间:2024-10-26 00:22:03
本文介绍了如何在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:

  • 创建XML文件 不幸的是,Android不包括为方便地创建XML 文件所必需的一切。最值得注意的是, javax.xml.transform 不可用,并且不推荐添加平台类。这意味着你必须在自己的转换类上写出它
  • 创建JSON文件 JSON是得到Android API的良好支持。请参阅此处和这里
  • 创建Java序列化文件 只要您的对象实现了Serializable接口,那么可以轻松地将写入文件。我从来没有需要这样做,但它应该是直接,网络上应该可以使用示例。 (例如,Google通过这个迷你教程)
  • 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
  • Create JSON files JSON is well supported by the Android API. See here and here
  • 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中保存数据结构?

    本文发布于:2023-10-14 22:42:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1492425.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据结构   如何在   Android

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!