my code here is terribly wrong, and I'm not sure how you will do it right. I have a Spinner that is populated from a SQLite database query through CursorAdapter. I need to get the text (value) of the currently selected item. I tried this garbage:
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition())
to get the text, but it crashes every time. What is the right way to do this? the following additional code may be useful here:
/// qc defined above as a SimpleCursorAdapter /////////setup product selection spinner from db prdSpn = (Spinner)findViewById(R.id.prd_spn); Cursor prdCur = null; try { prdCur = mDb.query(smsDbSchema.ProductSchema.TABLE_NAME, null, null, null, null, null, null); } catch(Exception e) { Log.e("smsdb", e.toString()); } prdCur.moveToFirst(); startManagingCursor(prdCur); qc = new SimpleCursorAdapter( this, android.R.layout.simple_spinner_item, prdCur, new String[] {smsDbSchema.ProductSchema.COLUMN_NAME}, new int[] {android.R.id.text1} ); qc.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); prdSpn.setAdapter(qc);
java android simplecursoradapter spinner
moonlightcheese
source share