Best method to store data in android application? [closed]
Asked Answered
O

2

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!

Outspan answered 11/6, 2013 at 9:2 Comment(6)
The easiest way is the best way. developer.android.com/guide/topics/data/data-storage.html But for predefined thingies you don't need anything else than xml resource files.Allspice
An SQLite database seems the right solution.Lindblad
i guess simple txt file would work for you, but if you want to do it "proper" way use SQLiteChenay
SQLite seems be the best, you can find a good example here (docs.huihoo.com/android/3.0/resources/samples/…). It is a searchable dictionnary with custom search, sql lite database...Conventionalize
If your quotes and poems are large in size, then go with text file. to do it in a proper way, map the file path with your file name in sqlite table.Tiercel
Related post - How to save data in an android appLiggett
T
8

It feels absolutely absurd to go for Sqlite, even if it's a thousand strings. Reading a plain text file one line at a time and storing the strings in a List or Array takes absolutely no time at all. Put a plain text file in /assets and load it like this:

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;
}

Alternatively go for JSON (or possibly XML), but plain text should be fine.

Tomika answered 11/6, 2013 at 9:18 Comment(2)
I just came across your answer and was wondering.. would this be more efficient than copying each line to a SQLite database even if there are, say, 20,000 words?Bonar
Even with 20k words it should be fast (not sure how fast, but I'm guessing milliseconds, not seconds). I would definitely try this first and look to other options if it proves to be slow.Tomika
F
1

I think it depends strongly on the amount of data...

For 1-100 strings, use xml in the application resource. For more, the better way (and the fastest) is sqlite!

Fremd answered 11/6, 2013 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.