开发者

Populating a spinner with a db query

开发者 https://www.devze.com 2023-03-26 18:29 出处:网络
I recently started programming for android and java development in general. Currently I train by writing a timetable app.

I recently started programming for android and java development in general. Currently I train by writing a timetable app. I want to get a list with all subjects from the db (th开发者_运维知识库e table also contains teacher and rooms) and put them into a spinner. Thats the code I wrote

Cursor c = dba.fetchAllSubjects();

if (c.getCount() != 0)  {
    Spinner subjectSpinner = (Spinner) findViewById(R.id.newlesson_subject);
    startManagingCursor(c);
    String[] from = new String[]{DbAdapter.KEY_SUBJECT}; 
    int[] to = new int[] {android.R.layout.simple_spinner_item};
    SimpleCursorAdapter subjectAdapter = 
        new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
    subjectSpinner.setAdapter(subjectAdapter);
}

The problem is that when clicking on the spinner it shows a list with as much items as the db has entries, but the list doesn't show the names (you can say it's empty). So I probably messed something up with the simple cursor adapter, but I don't know what.

Thanks for your help


You're error lies in specifying the wrong Resource ID in your to integer array.

int[] to = new int[] {android.R.layout.simple_spinner_item};

You need to specify the ID of a TextView (or similarly typed view) and not the layout itself. Replace the line above with:

int[] to = new int[] { android.R.id.text1 };
0

精彩评论

暂无评论...
验证码 换一张
取 消