1

これはおそらく単純な答えのばかげた質問ですが、メモ帳の例から単純なカーソルアダプターを使用すると、データベースから名前のリストを取得します。

行の上にカーソルを移動して名前を抽出することで「手動」で実行しようとすると、カーソルは行がゼロであると表示します...

これは例のように機能します。

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

今私はこれをやろうとしていますが、それは完全には機能していません。単純なカーソルアダプタは、私がしていない特別なことをしていますか?

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

私はこれを機能させましたが、「 _ 」という名前のすべての行を返すなどの特定のクエリを返します。すべてのメモを返すことの違いは何ですか?

4

1 に答える 1

0

moveToFirst()実際にはを返すので、 :booleanから読み取ろうとする前に値をチェックすることで、例外がスローされるのを防ぐことができます。Cursor

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

行が0である理由については、に渡したのと同じカーソルを再利用しようとしていますSimpleCursorAdapterか、それともスタンドアロンで失敗しているコードですか?Cursor再利用する場合は、フレッシュを実行した後、新しいものを使用して試してみfetchAllNotes()ます。

于 2011-02-19T00:46:59.893 に答える