tags:

views:

45

answers:

1

Hello, is it possible to populate a listview based on a row Id from a custom cursor adapter. I keep getting an error "illegalArguementException column: _id does not exist". but the database has it and is already being used correctly. I don't know what to do, because I would need to populate different listviews from the same database and i don't want to have to create multiple database which will still have the same column names. Any ideas will be greatly appreciated. thanks

HERE is the database code is being cold:

public Cursor retrieveItemRow(long Id){
        open();
String[] Columns = new String[] {TITLE,DATE, TIME};
Cursor row = db.query(true,DATABASE_TABLE, Columns, KEY_ID +"=" +Id, null, null, null, null,null);
    if(row != null){
    row.moveToNext();
    return row;
    }
    return row;
      }

Here is a method i am trying to call it in:

 public void fillRowData(long Id) {

Cursor cursor = adapter.retrieveRow(id);
startManagingCursor(cursor);
String[] from = new String[]{DBAdapter.TITLE, DBAdapter.DATE, DBAdapter.TIME};
int[] to = new int[]{R.id.Name, R.id.Date, R.id.Time};

customCursor items = new customCursor(this, R.layout.viewlist, cursor, from, to);
setListAdapter(items);          

}
A: 

Make sure you include _id in the SELECT statement represented by the Cursor you pass into your adapter. For example, if I were using the SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to), my Cursor c would originate with something like "SELECT _id, name FROM users;".

From the Notepad Tutorial:

private void fillData() {
     // Get all of the notes from the database and create the item list
     Cursor c = mDbHelper.fetchAllNotes();
     startManagingCursor(c);

     String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
     int[] to = new int[] { R.id.text1 };

     // Now create an array adapter and set it to display using our row
     SimpleCursorAdapter notes =
          new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
     setListAdapter(notes);
}

The tutorial defines a method in the database helper class that returns all of the Note objects. Essentially they're using SQLiteDatabase's query method that returns a Cursor object. The query's select string is where you should include the _id column.

So in your case, you need to add KEY_ID to your Columns String array variable, because those are the columns that are included in your select statement (db.query()). This is also hinted at in the CursorAdapter documentation:

The Cursor must include a column named "_id" or this class will not work.

McStretch
sorry to bother, but am slightly confused. do i include the select statement in the database class where am calling the cursor or in the adapter. please any link to an example on this would be appreciated.
Rexx
I just updated my initial response, check it out and let me know if that makes sense.
McStretch
i already have a listview that is populated from a listview. what i want to do now is populate another listview with the rowId i have already obtained. like Cursor c = mdbHelper.fetchNote(rowId). this is where i get the error.
Rexx
What are the select statements that you are using? Post all of the code that is useful in debugging the situation.
McStretch
i don't use any select statement in the code. i have added the database code that is causing the problem.
Rexx
Check out my edit -- the query() method is your abstracted select statement.
McStretch
ok.. thank you very much! will do that and keep you posted.
Rexx
oh thank you very much!! works great.. never payed attention to that statement "The Cursor must include a column named "_id" or this class will not work." and i had it in another method. Bad Student!! :(
Rexx
Haha awesome! Mark the answer as accepted when you get a chance to let others know that a solution was found.
McStretch