I have a spinner in my activity, populated from a DB, and if the activity gets an Intent with Extras, I want to set an Item from the spinner selected according to the extra. For example - if I have an Extra "CURR_NOTE_CATEGORY" and its value is 2, I want the second item in the spinner to be selected. No extras - just the regular spinner. So I have this code:
int curr_note_category;
ArrayAdapter<String> adapter;
NotesManager manager = new NotesManager(this);
ArrayList<Category> arrListCategories;
ArrayList<String> arrListCategoriesString = new ArrayList<String>();
Spinner spCategories;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_note);
spCategories = (Spinner) findViewById(R.id.spCategories);
if(getIntent().getExtras()!=null){
// Get Extras
curr_note_category = getIntent().getExtras().getInt("CURR_NOTE_CATEGORY");
spCategories.setSelection(curr_note_category);
}
manager.getAllCategories();
arrListCategories = manager.getAllCategories();
for (int i = 0; i < arrListCategories.size(); i++)
{
Category currCategory = arrListCategories.get(i);
arrListCategoriesString.add(currCategory.getCategory_name().toString());
}
adapter=new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, arrListCategoriesString);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCategories.setAdapter(adapter);
spCategories.setOnItemSelectedListener(spinnerListener);
}
I'm getting the regular spinner, but never a selected item when there's an Intent Extra. So how can it be done?