I have spinner which I populate from SQLite database. But I want to populate it from string-array:
<string-array name="category">
<item name="accommodation">Accommodation</item>
<item name="automobile">Automobile</item>
<item name="credit cards">Credit Cards</item>
<item name="donations">Donations</item>
<item name="entertainment">Entertainment</item>
<item name="food">Food</item>
</string-array>
where name I take from SQLite table.
I have method which retrieve name from database:
public Cursor getAllCategory() {
mDB = dbHelper.getReadableDatabase();
return mDB.query(CATEGORY_TABLE_NAME, null, null, null, null, null,null);
}
And in my program I use these names:
// spinner
catSpinner = (Spinner) view.findViewById(R.id.category_spinner);
cursor = adapter.getAllCategory();
String[] from = new String[] { DataAdapter.CATEGORY_COL_NAME };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter catAdapter = new SimpleCursorAdapter(
getActivity(),
android.R.layout.simple_spinner_dropdown_item, cursor,
from, to, 0);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpinner.setAdapter(catAdapter);
But I want use names from database, but value that responding to these names(string.xml) use from string-array
How I can do this? I want to do this to avoid changing database when I change localization.
Try this.For getting data into the spinner you have to use
SimpleCursorAdapterwhich is given below.And use this method in spinner to get items(i.e. Name).
Hope this helps. 🙂