LibGDX: how to save a large amount of data?

时间:2016-03-04 18:03:34

标签: sql json database csv libgdx

I am developing an application using LibGDX.

I would like to save a lot of sentences, each one with an index and the date when it was created.

The user should be able to find or select a specific sentence.

There should be sentences that are already created in the application (like in the assets folder) and the user should also be able to create his own sentences and to save them.

I am new to this topic and I dont know how to realize this idea. I found some things during my research but I am confused because there is much information and I do not know which one I need.

Should I use JSON files, csv files, a SQL database, or something else? And when, then how to do it with LibGDX?

I would be happy if you could give me some information on this topic or advice!

1 个答案:

答案 0 :(得分:2)

首选项是要走的路,除非你在谈论成千上万的用户句子。它还取决于scalablity,如果你只是想添加字符串,这就完成了工作。如果您需要查找特定字符串,则可能需要加载它们。首选项存储在本地,因此每个模块都可以通过Gdx界面轻松访问它。这是您使用Preferences

的方式
    Preferences preferences = Gdx.app.getPreferences("filename");       
    preferences.putString(Key<String>, Value);
    preferences.flush();

现在,为了能够继续添加字符串,每次在首选项中放入字符串时都需要增加键。您也可以在首选项中执行此操作。

    int stringCount = preferences.getInteger("string_count"); // Get current string count
    preferences.putInteger("string_counr", stringCount++); // Increment stringCount and put it back in preferences
    preferences.putString("userstring_" + stringCount, someString); // Create string using incremented stringCount.
    preferences.flush(); //Store data

您可以使用字符串计数来访问字符串。或者您可以在运行时加载它们。根据您的需要,您还可以将确切的字符串存储为密钥,但如果您不知道字符串,则很难检索它们。

    int stringCount = preferences.getInteger("string_count");
    List<String> userStrings = new ArrayList<String>();
    for (int i = 1; i <= stringCount; i++)
    {
        userStrings.add(preferences.getString("userstring_" + stringCount);
    }

字符串存储在users/username/.prefs/filename

的窗口中

我已将几百个字段存储到首选项中,没有任何问题。它不是flush()上最快的,但您可以存储所有句子并批量冲洗它们。如果您的需求不是太苛刻,那么偏好很容易。如果你寻找更多可扩展的选项,那么你应该使用JSON,XML,一种脚本语言,或者为它编写自己的读者/编写器。除非您想将用户和其他数据链接到这些句子,否则数据库将会过度杀伤。