Using SimpleCursorAdapter with Spinner?

前端 未结 3 1182
终归单人心
终归单人心 2021-01-13 12:24

I have a db with table \"mytable\" having 2 colums \"id\",\"sampletext\" I want to query distinct values of sampletext and feed to a Spinner using SimpleCursorAdapter.

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-13 13:03

    what am i doing wrong
    

    you didnt read documentation ...

    there are two arrays of string with columns: first in used in query, second one in Adapter constructor(you used only one array for both)

    first one tells sqlite which columns should be taken to Cursor, second tells Adapter which ones should be showed/mapped to Views in single row...

    Next CursorAdapter needs Cursor with column named _id

    So now it's pretty obvious that we should do smthin like this:

    String[] queryCols=new String[]{"_id", "sampletext"};
    String[] adapterCols=new String[]{"sampletext"};
    int[] adapterRowViews=new int[]{android.R.id.text1};
    mycursor=sdb.query(true,"mytable", queryCols,null,null,null,null,null,null);
    sca=new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, mycursor, adapterCols, adapterRowViews,0);
    sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spn.setAdapter(sca);
    

提交回复
热议问题