views:

63

answers:

1

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.

A: 

The problem, I think, was due to my cursor being recreated on every call. I don't have a better explanation. This post indirectly pointed me in the right direction.

By holding on to the cursor after creating it initially, I was able to just call requery() after changing the list data, as opposed to running through the method in my question. It works fine now.

cdonner