views:

398

answers:

2

I'm attempting to make a dynamic drop down that will be filled by a SQLite table. I have a Cursor object which I can pull the data I need from. I've been able to accomplish loading the values into the drop down with the code below:

Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    try{
        Cursor cursor = getAccounts();
        int accountnameIndex = cursor.getColumnIndexOrThrow(ACCOUNT_NAME);
        if(cursor.moveToFirst()){
            do{
                adapter.add(cursor.getString(accountnameIndex));
            } while(cursor.moveToNext());
        }
    } finally {
        MintLink.close();
    }

My problem is that I need the a selection from the drop down to also contain the RowID of the item selected. I need to be able to select one item and have access to the value of that item in the back end. For example, think of a drop down in HTML. Each drop down selection has it's own hidden value that is pulled. I need this value to be hidden for me to allow me to know which ID they choose.

A: 

Try using a SimpleCursorAdapter instead of copying all the data by hand into an ArrayAdapter.

CommonsWare
While this is in short the answer. What are the details in implementing the SimpleCursorAdapter to work with the Spinner? Do I need to abstract my own ViewBinder or can I used the SimpleCursorAdapter directly with the Spinner.
GrandPrix
There's no way to answer that in the abstract. I can point you at some sample code, if it helps (though it gets its Cursor from a ContentProvider, not SQLite): http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Spinners/
CommonsWare
A: 

Example of data binding can be found here.

http://developer.android.com/guide/topics/ui/binding.html

check "Filling the Layout with Data" section for usage of SimpleCursorAdapter

Anji