Carnivoris
Carnivoris

Reputation: 803

Spinner + Cursor issues

I honestly have no idea what the deal is with this one. It should be simple enough, but, for some reason, my spinner is not populating with any data.

    final String fields[] = { "siteName","_id" };
    int[] to = new int[] { android.R.id.text1 };

    DatabaseHelper helper = new DatabaseHelper(this);
    database = helper.getReadableDatabase();
    Cursor ftpCursor = database.query("ftptable", fields, null, null, null, null, null);
    startManagingCursor(ftpCursor);

    Log.i("tag", "getcount result:"+ftpCursor.getCount());

    SimpleCursorAdapter dataSource = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, ftpCursor, fields, to);
    dataSource.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    s = (Spinner) findViewById(R.id.upload_screen_spinner);
    s.setAdapter(dataSource);

The Log line returns a getCount() of 4 on the cursor, so I know the cursor has data in it. When the spinner opens, there is no data in it at all. I figure it must be something stupid but I just can't see it.

Upvotes: 0

Views: 300

Answers (2)

Carnivoris
Carnivoris

Reputation: 803

I finally got it working. I think the catalyzing fix was changing the layout in the SimpleCursorAdapter to android.R.layout.simple_spinner_dropdown_item

final String from[] = {  "siteName","_id" };
int[] to = new int[] { android.R.id.text1 };

DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getWritableDatabase();
Cursor ftpCursor = database.query("ftptable", from, null, null, null, null, null);

SimpleCursorAdapter dataSource = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, ftpCursor, from, to);

ftpCursor.moveToFirst();
database.close();


Spinner s = (Spinner) findViewById(R.id.upload_screen_spinner);
s.setAdapter(dataSource);

Upvotes: 1

Krylez
Krylez

Reputation: 17800

Your from array has two values while your to array has one field. Try removing the "_id" and see if it works.

Upvotes: 0

Related Questions