Using SimpleCursorAdapter with Spinner?

戏子无情 提交于 2019-12-01 05:03:15

问题


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.

here is what is tried

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

When i run this i get error at line 4 : id does not exist. when i changed first line to "id" the spinner got populated with id values. But i need "sampletext", what am i doing wrong? Appreciate any suggestions


回答1:


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);



回答2:


Here's an example with raw query. Please note the first ID column returned by the query should be labeled as _id .

MyDatabase.java:

public class MyDatabase extends SQLiteAssetHelper {
     ...
    public Cursor getListNamesForDropDown() {
        SQLiteDatabase db = getReadableDatabase();
        String sql = "select ID _id, Name from MyTable order by Name ";
        Cursor c = db.rawQuery(sql, null);
        c.moveToFirst();
        return c;
    }

MyActivity.java:

        @Override
        public void onCreate(Bundle savedInstanceState) {
....    
           Cursor cursorTest =  db.getListNamesForDropDown();
           android.widget.SimpleCursorAdapter  adapter = new android.widget.SimpleCursorAdapter(this, 
                   android.R.layout.simple_spinner_item,
                    cursorTest, 
                    new String[] {"Name"}, 
                    new int[] {android.R.id.text1}, 0);       
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           spinnerTest.setAdapter(adapter);  



回答3:


android.widget.SimpleCursorAdapter adapter = new android.widget.SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item,
            cursor,
            new String[] { DBOpenHelper.ACCOUNT_BANK },
            new int[] { android.R.id.text1 }, 0);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


来源:https://stackoverflow.com/questions/13413030/using-simplecursoradapter-with-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!