I'm creating an app that I want to seed with a data file the app will use as its initial state. In the Eclipse project structure, where do I add the data file so that when the app is deployed to the device (and emulator) the data file is deployed with it?
A:
It will be helpful it you can the data file type and the purpose. If you are looking to initalize certain settings you can do it through:
Describe preferences in xml --- store in RES/XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory >
<CheckBoxPreference />
</PreferenceCategory>
</PreferenceScreen>
Load for sure onCreate():
/* Loading default preferences the first time application is run */
PreferenceManager.setDefaultValues(getApplicationContext(),
R.xml.generalsettings, false);
Else,
You can hardcode data and store in the db:
String sql = "CREATE TABLE ..." + "INSERT VALUES ... INTO ... ";
db.ExecSQL(sql);
Else you can store this file in the Assets folder and use getAssets() to retrieve the file:
getAssets().open(fileName)
Eg. using raw data file. In case of assets replace with above command:
InputStream is = context.getResources().openRawResource(R.raw.artoodict);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
while ((readLine = br.readLine()) != null) {
}
}
} catch (IOException e) {
e.printStackTrace();
}
Sameer Segal
2010-08-30 17:15:04
The kind of data I'm thinking about isnt user settings. Its more like a list of locations, by state (200 - 300 locations per state). I'm still not sure how the app will request the data; via SQLLite call, or webservice call or what. But for now I was just gona put it in a data file (one file per state), and have the app just open the file and read the contents.But I cant figure out where to put the data files in the eclipse project structure, and how to push the data files to the app/emulator when debugging.
Turbo
2010-08-30 17:27:12
And even if I was going to use SqlLite, I need to initialize the database somehow, which will require 50*200 insert statements. So I guess my real question is how do you seed initial data required by an android app when first developing one?
Turbo
2010-08-30 17:28:31
Put it in the assest's folder. You can use the simple file read statements to extract the values. XML based input is another option.See edits above
Sameer Segal
2010-08-30 18:13:54