i have searched for a awnser for this for a while today. It all looks so easy but i never get it to work. I want to fill a spinner with my cursor. I have been trying to use SimpleCursorAdapter for this as a lot of sites say i shall but i never get it to work. Show me just how easy it is :)
Thanks for your time!
My cursor
Cursor cursor = db.query(DATABASE_TABLE_Clients, new String[] {"_id", "C_Name"}, null, null, null, null, "C_Name");
My spinner
(Spinner) findViewById(R.id.spnClients);
My Code
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { R.id.txt_Address };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, cursor_Names, columns, to);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);
The following code solved my problem. I was missing .setDropDownViewResource. After that i used simple_spinner_dropdown_item so i don't have to make my own layout.
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);
I don't see a View for your dropdown in your code. Something like:
mAdapter.setDropDownViewResource(R.layout.spinner_view_dropdown);
Of course you need to have a spinner_view_dropdown.xml file in your res/layout directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With