The other answers did not seem to answer how to actually populate the database with initial values so I will share how I did it here.
Basically you save your database columns as string arrays in an xml file. I will just show one array but you could do more for other columns. (You would need to be careful, though, with multiple columns not to mess up the array order, and thus messing up your database rows.)
Make a /res/values/arrays.xml file and add the array to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="my_array">
<item>string 1</item>
<item>string 2</item>
<item>string 3</item>
<item>string 4</item>
<item>string 5</item>
</string-array>
</resources>
Then you can get that array when you create the database in your helper class. Here is what I did:
private static class DatabaseHelper extends SQLiteOpenHelper {
private final Context fContext;
// constructor
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
fContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
+ FIRST_COLUMN + " INTEGER PRIMARY KEY,"
+ SECOND_COLUMN + " TEXT NOT NULL,"
+ THIRD_COLUMN + " INTEGER,"
+ FOURTH_COLUMN + " TEXT NOT NULL"
+ ");");
// Initialize the db with values from my_array.xml
final int DEFAULT_THIRD_COLUMN = 1;
final String DEFAULT_FOURTH_COLUMN = "";
ContentValues values = new ContentValues();
Resources res = fContext.getResources();
String[] myArray = res.getStringArray(R.array.my_array);
for (String item : myArray){
values.put(SECOND_COLUMN, item);
values.put(THIRD_COLUMN, DEFAULT_THIRD_COLUMN);
values.put(FOURTH_COLUMN, DEFAULT_FOURTH_COLUMN);
db.insert(TABLE_NAME, null, values);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
See these links for more help:
Another thing I have done is make the databases ahead of time and then just copy it to the database folder when the app is installed. That may work better if you have a large amount of data and you don't want to download the database from the Internet. If you want to copy your database then see this answer.