views:

15

answers:

1

Hello all,

I'm a newbie in android, working on list adapters right now. I know some type of list adaptors need _id column to process the data. I provide an _id column but I couldn't make it work. I follow notepad example, so here is my code :

I create the db by this statement :

private static final String DB_TABLE = "radios";
public static final String KEY_NAME = "name";
public static final String KEY_URL = "url";
public static final String KEY_ROWID = "_id";

private static final String DB_CREATE = "create table " + DB_TABLE +" ("+ KEY_ROWID +" integer primary key autoincrement, " + KEY_NAME + " text not null, "+ KEY_URL +" text not null);";

Using a dbhelper class :

 private static class DBHelper extends SQLiteOpenHelper {

    DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DB_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
        onCreate(db);
    }
}

Here I get all the records in db :

public Cursor getAllRadios() {
    return db.query(DB_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_URL}, null, null, null, null, null);
}

And my adapter :

String[] from = new String[] { DBAdapter.KEY_NAME };
    int[] to = new int[] { R.id.text1 };

    try{
     SimpleCursorAdapter radios = new SimpleCursorAdapter(this, R.layout.list_item, cursor , from, to); 
     setListAdapter(radios);
    }
    catch(Exception e){
     e.printStackTrace();
    }

I couldnt find any problems but I still get no such column as "_id" error. Any ideas?

A: 

Hmm, your SQL code for creating the table looks fine to me (even if you could enclose your column names in quotes just to be sure). But maybe you already have an existing old table which is not upgraded? Just to be sure: increment DB_VERSION and execute again.

mreichelt