Spinner does not display the selected item in android

I use Spinner in my application, and when I resume from another screen, it displays the first element in it, but the old element is selected. I am using the following code, Anyone please help me.

Spinner= (Spinner) findViewById(R.id.spinner1);
dataAdapter=null;
dataAdapter = new ArrayAdapter<String>(this,android.
     R.layout.simple_spinner_item, country);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(dataAdapter);
if (myPrefs.getBoolean("isChecked", false)) {
    Spinner.setSelection(myPrefs.getInt("PreviouslyselectedID", 0));
}
else
{
    country.add(0, "Please Select");
}

Thanks in advance.

+5
source share
2 answers

When dynamically adding an element to the adapter, if you do not call:

adapter.notifyDataSetChanged();

the selection of the added item will not be displayed in the user interface.

+6
source

I faced the same problem with a fragment, maybe it will help you. Try adding this line below the adapter set line, Spinner.setSaveEnabled (false);

0
source

All Articles