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.
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....
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));
Get spinner selected items text?
List<String> list = new ArrayList<String>();
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 <String> 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
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 :)
Hope this helps!!
Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();