开发者

Fetch all notes returns zero rows from Android notepad example

开发者 https://www.devze.com 2023-02-11 19:52 出处:网络
Well this is probably a stupid question with a simple answer but when using simple cursor adapter from the notepad example, I get a list of names from my dat开发者_StackOverflowabase.

Well this is probably a stupid question with a simple answer but when using simple cursor adapter from the notepad example, I get a list of names from my dat开发者_StackOverflowabase.

When I try to do it "manually" by moving the cursor over the rows and extracting the names the cursor says there is zero rows returned...

This works as per the example:

Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only NAME)
    String[] from = new String[]{WeightsDatabase.KEY_NAME};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.weightrows};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.weights_row, notesCursor, from, to);
    setListAdapter(notes);

Now i'm trying to do this but it's not quite working. Is the simple cursor adapter doing something special i'm not?

//check # of rows, returns 0
    int mOne = notesCursor.getCount();
    //initial random string array
    String[] temp = new String[100];
    int i = 0;
    notesCursor.moveToFirst();
    do{

        temp[i]=notesCursor.getString(notesCursor.getColumnIndex(WeightsDatabase.KEY_ROWID)); //crashes here
        //index out of bounds
        i++;
    }while(notesCursor.moveToNext());

I have gotten this to work, but with returning a specific query like return all row with the name "_". What is different about returning all notes?


moveToFirst() actually returns a boolean, so you can prevent the exception from being thrown by checking the value before you attempt to read from the Cursor:

if (notesCursor.moveToFirst()) {
    // do loop
}

As for why there are 0 rows, are you attempting to re-use the same cursor that you passed into the SimpleCursorAdapter, or is the code that is failing stand-alone? If you are attempting to re-use it, I would try it using a new Cursor after performing a fresh fetchAllNotes().

0

精彩评论

暂无评论...
验证码 换一张
取 消