6

I am new at Android programming and trying to create an application which shows up pre-defined quotes, poems etc. I want to know what is the best way to store these strings in the application? SQLite Database, XML file, text file? Any suggestions for cloud storage and how to achieve that in an android app?

Thanks in advance!

4

2 回答 2

8

使用 Sqlite 感觉绝对是荒谬的,即使它是一千个字符串。一次读取一行纯文本文件并将字符串存储在 List 或 Array 中绝对不会花费任何时间。将纯文本文件放入 /assets 并像这样加载它:

public List<String> readLines(String filename) throws IOException {
    List<String> lines = new ArrayList<String>();
    AssetManager assets = context.getAssets();
    BufferedReader reader = new BufferedReader(new InputStreamReader(assets.open(filename)));
    while(true) {
        String line = reader.readLine();
        if(line == null) {
            break;
        }
        lines.add(line);
    }
    return lines;
}

或者选择 JSON(或可能是 XML),但纯文本应该没问题。

于 2013-06-11T09:18:35.150 回答
1

我认为这很大程度上取决于数据量......

对于 1-100 个字符串,请在应用程序资源中使用 xml。更多,更好的方法(也是最快的)是 sqlite!

于 2013-06-11T09:05:45.337 回答