Best method to store data in android application?

12,469

Solution 1

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.

Solution 2

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!

Share:
12,469
Keya
Author by

Keya

Android Developer. Hoping to help out people here and learn more stuff from the intellectuals and proficient people in the field.

Updated on June 05, 2022

Comments

  • Keya
    Keya about 2 years

    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!