1

在一张桌子上,我这样做:

public static String[] FROM_Benzinarii_onList = {DBConstants.Denumire_Benzinarie, DBConstants.Adresa, DBConstants.Distance, _ID};

public Cursor getStations_List(){
        db = helper.getReadableDatabase();
        return db.query(DBConstants.TABELA_BENZINARII, FROM_Benzinarii_onList, null, null, null, null, null);

然后构造适配器:

int[] values = {R.id.tv_benzinarie,R.id.tv_adresa,R.id.tv_dist}
Cursor cursor = getStations_List();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_list, cursor,FROM_Benzinarii_onList, values);
     setListAdapter(adapter);

但是现在我需要一个额外的列,价格,来自另一个表,我无法获得游标。我已经尝试了下面的代码以及许多其他代码。

public static String[] FROM_Benzinarii_onList = {DBConstants.Denumire_Benzinarie, DBConstants.Adresa, DBConstants.Distance, DBConstants.Pret, _ID};

public Cursor getStations_List(){
        db = helper.getReadableDatabase();
        String sql = " select " + DBConstants.TABELA_BENZINARII + "." + DBConstants.Denumire_Benzinarie + "," + DBConstants.TABELA_BENZINARII + "." + DBConstants.Adresa 
                + "," + DBConstants.TABELA_BENZINARII + "." + DBConstants.Distance + "," + DBConstants.TABELA_PRETURI + "." + DBConstants.Pret 
                + " from " + DBConstants.TABELA_PRETURI + "," + DBConstants.TABELA_BENZINARII
                + " where " + DBConstants.TABELA_BENZINARII + "." + DBConstants.ID_Benzinarie + " = " + DBConstants.TABELA_PRETURI + "." + DBConstants.ID_Benz;
        return db.rawQuery(sql, null);
    }

然后用同样的方法构造SimpleAdapter(不知道对不对!?)

int[] values = {R.id.tv_benzinarie,R.id.tv_adresa,R.id.tv_dist,R.id.tv_pret};

Cursor cursor = getStations_List();

SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_list, cursor,FROM_Benzinarii_onList, values);
        setListAdapter(adapter);

我得到:

 Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

我不知道我的新光标是否与我有关,FROM_Benzinarii_onList但我不能db.query在这里使用请帮助!

4

2 回答 2

0

您绝对需要在结果 Cursor 中有一个 _id 列才能在(简单)CursorAdapter 中使用它

于 2012-04-20T15:05:40.217 回答
0

看我的回答Android专栏_id不存在问题

尝试在我的答案中使用alias例如。select oid as _id...

于 2012-04-20T15:12:48.213 回答