I have a view with a spinner. The activity starts another acvitity with a popup where I add or delete values that the parent shows in the Spinner. So, in onActivityResult() I refresh the content of the Spinner so that it reflects any additional or deleted values, by calling my fillSpinner() method. The parameter to this method is the previously selected value:
private void fillSpinner(String value){
Cursor c = mDbHelper.getAllCategories();
startManagingCursor(c);
c.moveToFirst();
String[] from = new String[]{DBAdapter.KEY_CATEGORY};
SimpleCursorAdapter scCats = new SimpleCursorAdapter(
this, android.R.layout.simple_spinner_item,c,from,
new int[]{android.R.id.text1});
scCats.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(scCats);
if (value != null && value != "") {
category.setSelection((int)mDbHelper.categoryIndex(value));
}
}
When I open the Spinner, it contains the correct list (i.e. it was refreshed) and the correct value is selected. However, the Spinner control itself (in its closed state) does not show the selected value, but the first in the list.
When I step through the code in the debugger, the Spinner value is correct before and after I call setSelection() (and it is always called with the same correct id). However, since I cannot step out of the event, when I resume the execution after a short moment the value in the Spinner changes.
In other words, the spinner's displayed string is changed and is different from the selected item when I return from my popup activity.
Any ideas are appreciated.
I found a simple solution to that problem: Use the form Spinner.setSelection(int position, Boolean NavigateTo) to have the Spinner show the correct selected item.
Example: Spin.setSelection(iPos, true);
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With