1

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?

Igal
  • 5,833
  • 20
  • 74
  • 132

1 Answers1

2

Please put your spinner set selection code after setOnItemSelectedListener();

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);

if(getIntent().getExtras()!=null)
{
    // Get Extras
    curr_note_category = getIntent().getExtras().getInt("CURR_NOTE_CATEGORY");

    spCategories.setSelection(curr_note_category);
}
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Yep, I moved also the `manger.getAllCategories(); arrListCategories = manager.getAllCategories();` and the loop. I had a crash, but after reviewing the LogCat, I changed the curr_note_category assignment to `curr_note_category = getIntent().getExtras().getInt("CURR_NOTE_CATEGORY")-1;` and now it works! Thank you very much! :) – Igal Aug 25 '12 at 10:21
  • Well, after further testing I see that it doesn't work smoothly. Here's the problem - the code I've mentioned above works fine if I have all the items in the DB by order: 1, 2, 3, 4 etc... But if I delete a category and add another one, (so that now the order of the IDs is, for example, 1, 2, 4, 5, 6 etc), then in the latter categories the app crashes. I'm getting this error message: " java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4". I've uploaded the full log text here: [link](https://dl.dropbox.com/u/30043022/log.txt) How do I fix it? – Igal Aug 25 '12 at 15:23