0

I have been learning Android development for a little while now and am in the process of creating a simple app to get started. The app is a simple quiz that will show a question and a text box to answer the question.

The framework (Libgdx) im working with doesn't have support for Sqlite so i can't use that to store all the question and answers. I am wondering how else i can store a mass amount of questions, answers without using a database and is it efficient to use arrays.

0

3 Answers 3

1

You can either save those "large amount of Questions and Answers" in a txt file .. and read them from there.
OR
you can use SharedPreferences
Here is one line example to save values
SharedPrefences.Editor

editor.putString(editKey.getText().toString()); // putString is the method of Editor class, and in this example editkey is the field for EditText

This value will be stored even after the app has been closed.

Sign up to request clarification or add additional context in comments.

2 Comments

In libgdx you can use them with the Preferences class. The Preferences are the only way to make things persistent in HTML5 applications. But they are most likely used for settings (like volume, graphic quality...). For this kind of things i would use a txt, as an XML, Json or CSV format.
I know Preferences class is the easy way .. That depends on how large is "large amount of questions" .. but you're rite about XML or txt file.. much simple and maintainable way..
1

In libdx you don't use a sqlite you simply use the preferences they offer. They get stored on android in the SharedPreferences and on desktop in an xml. For storing the questions i would hardly recomend to use a xml format and parse it on runtime. Maybe even encrypt/decrypt it.

To give you an idea what we are talking about. You create an XML maybe with an editor you just shortly hacked. Parse it into an good looking xml format. For example this is one of mine (not for questions but i think you get an idea of how to use the idea)

<root>
  <quest1 id="1">
    <name>quest1_name</name>
    <description>quest1_description</description>
    <subq type="KILL" monster_type="death" count="1" reward="15" />
  </quest1>
  <quest2 id="2">
    <name>quest2_name</name>
    <description>quest2_description</description>
    <subq type="KILL" monster_type="silverbat" count="25" reward="45" />
  </quest2>
</root>

Now you create an QuestionManager which you start while starting your app. And this does parse all childs of the root Element into an Question. So your Question has an constructor which takes an Element. This is the loading inside of the Manager:

public void loadQuests() {
    XmlReader r = new XmlReader();
    Element e = null;
    try {
        e = r.parse(Gdx.files.internal("quest/quest.xml"));
    }
    catch (IOException a) {
    }

    if (e != null) {
        for (int i = 0; i < e.getChildCount(); i++) {
            m_quests.add(new Quest(e.getChild(i))); //has reference to the core class
        }
    }
}

And the Quest (in this case it's not a question i know!)

public Quest(Element xml) {
        setID(xml.getInt("id"));
        setTitle(xml.getChildByName(Quest.NAME).getText()); // final static string 
        setDescription(xml.getChildByName(Quest.DESC).getText());
}

Even a thousand of questions shouldn't be such a big deal. In case it does let your Game lag on startup put the loading in an sperat thread for it and don't start using the QuestionManager till the loading is done or you synchronize the list / array of question. Also use the collections of libgdx to have a better performance and less garabage collecting.

If you really want to use a sqllite on Android go for Interfacing with platform specific code. But you cant use that on desktop than! So you need to create a desktop and an android implementation then.

1 Comment

Nice explanation (i guess you are programming an RPG kind of game :P). Definitely diserves a +1. The XML gives him the posibility to use an attribute for the right answer (<Answer correct="1">The Answer </Answer><Answer correct="0">Wrong Answer</Answer>).
1

Disclaimer: I'm not familiar with Android development.

The alternative to using a database would probably be serializing and deserializing your Java objects to files. JSON (Example library: https://code.google.com/p/google-gson/) or XML (Example library: http://x-stream.github.io/) are file formats that are a good fit for this approach.

1 Comment

Good point. Libgdx supports XML and Json with some useful methods, with whcih you can easily create and parse XML/Json files. But for his needs maybe some kind of CSV would be better, as he does not really need a complex strukture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.