0

良い一日、ティルトが誤解を招かないことを願っています。以下のコードスニペットを見て、コメントされた部分に注目してください。

//if(cursor.moveToFirst()){
if(cursor.moveToNext() == true){

         // do {
                Log.d(TAG, "Column Name in bindview is " + columnName);
                String name = cursor.getString(cursor.getColumnIndexOrThrow(columnName));

                Log.d(TAG, " name is " + name);


           // } while(cursor.moveToNext());
       //}
   }

今、私が使用するときだけcursor.moveToNext()、文字列の値を取得します。上記のコードまたはcursor.moveTofirst()でコメントアウトされているステートメントを"name"使用すると、文字列のnull値を取得します。do/whileなぜこれが起こっているのか考えてみてください。

*背景CursorAdapter *からこれを呼び出し/初期化しonLoadFinished()ていCursorLoaderます。

4

1 に答える 1

0

おそらくあなたはこれを行おうとしています:

// Get the column's index
int index = cursor.getColumnIndex(columnName); 
String name;

// You might want to check if the column exist with: 
// if(index == -1)
//    return;

// If you have move the Cursor's index, reset it:
// cursor.moveToFirst();

while(cursor.moveToNext()) { // == true is implied
    name = cursor.getString(index);
    Log.d(TAG, " name is " + name);
}

最初にループの外側の列インデックスを見つけます。カーソルを変更しない限り、列インデックスは変更されません。次に、すべての有効な結果をループします。

于 2012-07-31T22:15:24.283 に答える