Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 933
Keya Avatar asked Jun 11 '13 09:06

Keya


2 Answers

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.

like image 116
britzl Avatar answered Oct 29 '22 23:10

britzl


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!

like image 43
Mithenks Avatar answered Oct 30 '22 00:10

Mithenks