setSelection on Spinner based on rowId

前端 未结 7 762
离开以前
离开以前 2020-12-14 22:19

I have a Spinner View that\'s populated through a SimpleCursorAdapter.

Based on the selection I need to save the rowid in the entry database (posit

7条回答
  •  余生分开走
    2020-12-14 22:34

    Why do it the hard way when you can do it the right way?

    I refer to the manual:

    http://d.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29

    example code:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView parent, View view, int position, long id)
            {
                int index = spinner.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), 
                    "You have selected item : " + index + " which is row " + id, 
                    Toast.LENGTH_SHORT).show();                
            }
            public void onNothingSelected(AdapterView arg0) {}
        });
    

    Thanks to evancharlton on #android-dev for this enlightment. :)

提交回复
热议问题