2

I am stuck with SimpleCursorAdapter, I am calling my local SQLite DB and putting it into a cursor which then is passed to the SimpleCursorAdapter.

For same reason the Log Cat keeps showing this error below. I have no idea what is going on and I have been working on this for 6 hours, I didn't think SimpleCursorAdapter would be so difficult to understand.

05-28 19:47:27.524: ERROR/AndroidRuntime(9353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyneaxis.android.mpg/com.nyneaxis.android.mpg.userInfo}: java.lang.IllegalArgumentException: column '_id' does not exist


        setArray();
    rec.open();
        Cursor c = rec.getAllVeh();
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.userinfo, c, new String[]{c.getString(1)}, new int[]{R.id.nameTxtL});
        this.setListAdapter(adapter);
    rec.close();

//data adapter

public Cursor getAllVeh() {
    try{
    return db.query(database_table, new String[] { key_rowid, vehicle_name,
            year, make, model, style, vin, plate, notes }, null, null,
            null, null, null);
    }finally{

    }
}

Okay I have modified my code to a rawQuery and I get this error again:

05-28 22:41:48.876: ERROR/AndroidRuntime(1359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyneaxis.android.mpg/com.nyneaxis.android.mpg.userInfo}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0

private static final String db_sel = "SELECT id as _id, vehicle_name FROM vehicle";

public Cursor getAllVeh() {
        try{
            return db.rawQuery(db_sel, null);
        /*return db.query(database_table, new String[] { key_rowid, vehicle_name,
                year, make, model, style, vin, plate, notes }, null, null,
                null, null, null);*/
        }finally{

        }
    }
Brandon Wilson
  • 4,462
  • 7
  • 60
  • 90

3 Answers3

3

See my answer to this question Android: column '_id' does not exist problem. It explains about the need for the _id column and how to alias it if your DB tables don't have a column with that name.

****EDIT:**** To alias the column in the DB which contains 'unique identifiers' you need to use db.rawQuery(...) instead of db.query(...). The db.rawQuery(...) method takes a SQL string which will allow you to alias the column name to '_id' which is required by the adapter. Example...

Cursor c = db.rawQuery("SELECT <my_unique_column_name> as _id, vehicle_name, ... FROM vehicles");

In the above, replace <my_unique_column_name> with the actual name of the column in the vehicles table which contains unique identifiers. Also, use the actual column names for any other columns that you're requesting the data for.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Now I get this error: 05-28 20:17:09.634: ERROR/AndroidRuntime(10305): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, vehicle_name, year, make, model, style, vin, plate, notes FROM vehicle – Brandon Wilson May 28 '11 at 20:22
  • @Brandon: Use an alias, i.e., "SELECT id as _id,..." etc – Squonk May 28 '11 at 20:30
  • I'm sorry if I am not understanding this. Is there a way to provide an example? I am really not getting this. – Brandon Wilson May 28 '11 at 20:35
  • I have used the alias and I still get the error. Here is my code for retrieving the data. above – Brandon Wilson May 28 '11 at 20:59
  • @Brandon: Perhaps it's me who isn't understanding. Can you confirm if the table has a column called '_id' or is the column called 'id' (your comment on Femi's answer made me think it has a column called 'id'. Could you edit your original question to show the code for your Cursor query? – Squonk May 28 '11 at 21:00
  • I have added _id to the table and referenced it that way and still no luck. I have also used the alias to reference the column and yield the same results. – Brandon Wilson May 28 '11 at 23:10
1

Well, as the error says your database table definition is missing the default _id column that SimpleCursorAdapter expects to use as the ID column. What is your database table defined as? Add the CREATE TABLE statement you are using to your question.

SimpleCursorAdapter relies on the presence of an _id column: if you don't have it then you'll get that error.

Femi
  • 64,273
  • 8
  • 118
  • 148
0

Thanks guys for all you help. I managed to do it a different way and it seems to work like a charm. If anyone has any question or suggestions let me know.

Cursor c = rec.getAllVeh();

while (c.moveToNext()) {
String vehName = c.getString(1);
vehInfo.add(vehName);
}

//put information into the addapter for listview
adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_single_choice, vehInfo);

//applies adapter to listview
list.setAdapter(adapter);
Brandon Wilson
  • 4,462
  • 7
  • 60
  • 90
  • Hi, your approach looks really good to me, but can you please let me know what is vehInfo, is that StringBuilder, StringBuffer or vector or some list what is that? – Shaista Naaz Jul 01 '11 at 17:18
  • And how do u set these strings to the particular ids defined in the xml? – Shaista Naaz Jul 01 '11 at 17:19