Wednesday, January 5, 2011

[android-developers] create spinner with "add item" selection

I have a spinner that is populated by an sqlite db.  However when the app is first used and the cursor is empty, I want the spinner to offer an option to add a new item. In fact I would like this option to always be available at the end of the spinner list, but at this point I will settle for just when the cursor is empty.  The below code shows what I have tried.  Although the OnItemSelected method is called when the spinner is created.  Therefore since the only item is the "Add Item" item, NewItem gets called on creation instead of when the user selects it.  This would be fine because I could just ignore the onItemSelected call on creation, but because the item is selected, when the user then tries to select "Add Item", the OnItemSelectedListener ignores the selection (which is wrong by the way, it should be up to the code to do the ignoring not the widget, but that is another topic).

if (_spinnerAdp.isEmpty())
{
   // GetAddItemAdapter just returns an ArrayAdapter of 1 string
   _spinnerAdp.setAdapter(GetAddItemAdapter(this));  
   _spinnerAdp.setSelection(-1);
   _spinnerAdp.setOnItemSelectedListener(new OnItemSelectedListener() 
   {
 public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) 
 {  
NewItem()
 }
 public void onNothingSelected(AdapterView<?> arg0) 
 {
//when does this happen?
 }
   });              
}

Does anyone have a method to do this?  I have seen "add item" selections in spinners before in other UI Frameworks and think it is intuitive way to get this done.  I am not stuck on a spinner though if there is another widget that is easier to get this same thing done...

Thanks

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment