Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill spinner from cursor in android

Tags:

android

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);
like image 584
Rickard Avatar asked Apr 20 '10 12:04

Rickard


2 Answers

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);
like image 140
Rickard Avatar answered Oct 12 '22 09:10

Rickard


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.

like image 39
Maurits Rijk Avatar answered Oct 12 '22 09:10

Maurits Rijk