Spinner Update

I have a view with a counter. This activity launches another acquitability with a pop-up window where I add or remove values ​​that the parent displays in Spinner. Thus, in onActivityResult (), I update the contents of Spinner so that it reflects any additional or deleted values ​​by calling the fillSpinner () method. The parameter of 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 Spinner, it contains the correct list (that is, it has been updated) and the correct value is selected. However, the Spinner control itself (in the closed state) does not display the selected value, but the first in the list.

When I look at the code in the debugger, the Spinner value is correct before and after calling setSelection () (and it is always called with the same valid id). However, since I cannot exit the event when I resume execution after a short moment, the value in Spinner changes.

In other words, the line with the counter reading changes and differs from the selected item when I return from my pop-up activity.

Any ideas are welcome.

+7
android android-activity spinner
source share
2 answers

The problem, I think, was that my cursor was recreated with every call. I have no better explanation. This post indirectly pointed me in the right direction.

Holding the cursor after it was created initially, I was able to simply call requery () after changing the list data, and not run through the method in my question. Now it works great.

+1
source share

I found a simple solution to this problem: use the Spinner.setSelection (int position, Boolean NavigateTo) form so that Spinner displays the correct selected item.

Example: Spin.setSelection (iPos, true);

Good luck.

+5
source share

All Articles