Reputation: 85
I need to put some default data to database. I made method in my databasehelper:
private void putDefaultList(SQLiteDatabase db){
String nazwaa = null;
String mark = "0";
ArrayList<String> nazwa = new ArrayList<>();
nazwa.add("jjjjjj");
nazwa.add("kokoko");
for (int i =0; i<nazwa.size(); i++) {
nazwaa = nazwa.get(i);
db.execSQL("INSERT INTO " + TodoTable.TABLE_NAME + " (" + TodoTable.COL_2 + ", " + TodoTable.COL_3 + ") VALUES (" + "'" + nazwaa + "'" + ", " + "'" + mark + "'" + ")");
}
This method works but i need to have different languages so the simplest way is to get access to strings.xml but I don't know how in this situation.
I tried Resources.getSystem().getString(R.string.jjjjjj)); but this crash my app.
Upvotes: 2
Views: 1007
Reputation: 9655
public class DbHelper extends SQLiteOpenHelper {
private Context context;
public DbHelper(Context context, String name, int version) {
super(context, name, null, version);
// Application Context
this.context = context.getApplicationContext();
}
@Override
public void onCreate(SQLiteDatabase db) {
String res = this.context.getString(String_resId);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Upvotes: 4
Reputation: 26084
You can create a class App like:
public class App extends Application {
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
@Override
public void onTerminate() {
super.onTerminate();
}
public static Context getContext() {
return mContext;
}
}
Then, in AndroidManifest.xml:
<application android:name="App"
<!-- More properties-->
>
</application>
Now, in your database helper you can obtain the context this way:
Context context = App.getContext();
And the desired String:
String myString = context.getResources().getString(R.string.jjjjjj);
Hope it helps!
Upvotes: 0