I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter spinnerAdapter =
new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
actionBar.setSelectedNavigationItem(targetIndex);
}
Above if block is called always. Even after setting index 2, next time I check it returns 0.
Edit: I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?
Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.
Here is an example:
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);
It works in my app without any problems.
Have you tried using shared prefences to save the value of the selected spinner. I used this code to save the users selection with shared preferences so the next time they opened the app the spinner was set to the last value they chose:
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getSelectedItem();
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = Filter.this.getSpinnerval(selected);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> parentView) {}
});
Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
Spinner.setSelection(position, true);
}
Then the get Util:
public String getSpinnerval() {
return getSharedPreferences().getString("val", "");
}
I altered the code a little so it might not be 100% right but may give you an idea.
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