1

On a single table I do it like this:

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

Then construct the adapter:

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

But now I need an extra colum, with price, from another table, and I am unable to obtain a cursor. I have tried the code below, and many others.

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

Then construct the SimpleAdapter in the same way (I don't know if it is correct !?)

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

I get:

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

I don't know if my new cursor has anything to do with FROM_Benzinarii_onList but I couldn't use db.query here Please help!

AlexAndro
  • 1,918
  • 2
  • 27
  • 53

2 Answers2

0

You absolutely need an _id column in your result Cursor to use it in a (Simple)CursorAdapter

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • Ok, but how can I do it? I specified it in the results String array, but it seems that has nothing to do with the cursor – AlexAndro Apr 20 '12 at 15:07
  • In your String[], there is a element named _id. it must be present in the cursor. Therefore, it must be present in the SELECT. – njzk2 Apr 20 '12 at 15:11
0

See my answer Android column _id does not exist problem

Try using an alias such as select oid as _id... as in my answer.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135