-1

What is the best way to access static data for an Android app

Information about the data

  • Will be read-only, the user cannot change it
  • Data will consist of an ID, and a few words/short phrases
  • It won't be huge: maximum of about 1000 words/short phrases
  • The data will be accessed sequentially
  • I will have the data while developing the app
  • The data will be read frequently

From what I've read on the web so far, I have a few options:

  1. Load all the static data into an SQLite database when the app starts. This is the secure way of doing it because then only rooted users can delete/change data. Also, Android allready has SQLite and querying is easy. Can be read sequentially with cursor.moveToNext() method
  2. Read the data from a flat/xml/csv file from within the app's resources. I don't know how the speed of reading from a file compares to using SQLite? Also, if I've got to reference to a particular line (which will be a word/phrase) in the file, what is the difference between that and just hardcoding it?

What is the best option? Are there better solutions?

Code Vader
  • 739
  • 3
  • 9
  • 26

2 Answers2

1

You can put your static data in values/strings.xml file. It is best way if you don't have huge data and user can not change it.

Android
  • 179
  • 1
  • 7
1
static final data ? :

The common way to do it in android , is to create a file called strings.xml under /res in your application, and to put your strings there. every string has a name. exemple :

<string name="app_name">Single Finder</string>

Then you can access to your strings from your XML layouts or from your java sources. exemple :

XML:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

Java:

String myStr = getApplicationContext().getRessources().getString(R.string.hello_world);

Another way, which is the Java common way to create static final strings. see Declaring a Variable as a Constant.

ahmed_khan_89
  • 2,755
  • 26
  • 49