Getting String value from Spinner supported by CursorAdapter from SQL query in Android

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); 
+6
java android simplecursoradapter spinner
source share
2 answers

The code is similar to the following works for me ...

 Cursor theCursor = qc.getCursor(); String theString = theCursor.getString(theCursor.getColumnIndex(<your column name here>)); 

EDIT by moonlightcheese:

implementation:

 Cursor theCursor = (Cursor)prdSpn.getSelectedItem(); Log.e("spnERRtest", "Item: " + theCursor.getString(theCursor.getColumnIndex(smsDbSchema.ProductSchema.COLUMN_NAME))); //theCursor.getString(theCursor.getColumnIndex(smsDbSchema.ProductSchema.COLUMN_NAME)).contains("CAR") 
+7
source share

((Cursor) prdSpn.getItemAtPosition (prdSpn.getSelectedItemPosition ())). GetString (prdSpn.getSelectedItemPosition ())

It is not clear why you are passing getSelectedItemPosition() to getString() . Should I not pass the column number of the column in which there is data? Isn't this column related to the row selected in spinner?

0
source share

All Articles