Android Spinner Selected Item

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

I am populating a spinner from the database like this

    // Populating the City Spinner     Cursor cities = db.cityList();     startManagingCursor(cities);      // create an array to specify which fields we want to display     String[] from = new String[] { DBAdapter.KEY_NAME };     // create an array of the display item we want to bind our data to     int[] to = new int[] { android.R.id.text1 };      Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     cityList.setAdapter(adapter); 

When i try to get the content from the selected item of the spinner like this

// Get the City                 Spinner getCity = (Spinner) findViewById(R.id.citySpiner);                 String cityName = getCity.getSelectedItem().toString(); 

i get the following. Is there a way i can get the city name or the city id from the database.

回答1:

I think, as you are using a customadapter and giving three lists in adapter...

you can't get the selected text simply by calling the getSelectedItem()..

Use this:

Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner); int position = mySpinner.getSelectedItemPosition(); String Text = yourCityList[position].toString(); // dont know which list is holding the city list...  // i didn't use any db in any of my app so cant tell you how can you get list...  // leaving it to you... :) 

Hope it helps....



回答2:

Just get the adapter from your spinner and get the string from the cursor

Cursor cursor = (Cursor) myAdapter.getItem(position); String cityName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME)); 


回答3:

Get spinner selected items text?



回答4:

    List list = new ArrayList();     s = (Spinner)findViewById(R.id.spinner1);     SQLiteDatabase sqldb = db.openDataBase();     Cursor cr = sqldb.rawQuery("select Name from employee",null);     if (cr.moveToFirst()) {         do {         list.add(cr.getString(0).toString());         Toast.makeText(this,cr.getString(0).toString(),Toast.LENGTH_LONG).show();         ArrayAdapter  a= new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);          a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);           s.setAdapter(a);         } while (cr.moveToNext());        }//urvesh patel 


回答5:

The way I achieved it was like this:

You will have to cast the getItem() method to a cursor as it is essentially just returning a cursor rather than an individual row in the cursor which is annoying.

final Cursor cursor = (Cursor) yourSpinner.getAdapter().getItem( position ); 

Move the cursor to the first row

cursor.moveToFirst(); 

Now you can get the string from the cursor using the column name that matches table originally queried when setting your spinners adapter which was grabbed above using getAdapter()

final String selectedPartUUID = cursor.getString( cursor.getColumnIndex( TABLE_COLUMN_NAME ) ); 

Hope this helps and would appreciate any feedback :)



回答6:

Hope this helps!!

Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();             Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show(); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!