2012-07-31 53 views
0

美好的一天,希望傾向不會讓人誤解。現在只有當我使用cursor.moveToNext()我能得到一個值的字符串"name"。如果我使用do/while語句在上面註釋掉奇怪的行爲從遊標查詢中檢索值

//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.moveTofirst(),我得到空值的字符串。任何想法爲什麼發生這種情況。

* 背景 *打電話/從CursorLoaderonLoadFinished() initalizing這個CursorAdapter

回答

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

首先找到外循環列索引,除非你改變光標列索引不會改變。然後遍歷所有有效的結果。

+0

謝謝你!但是這個陳述你做了「接下來我們假設這是一個新的Cursor指向-1,否則添加cursor.moveToFirst()」有點混淆,你沒有在你的代碼中這樣做。再次感謝 – irobotxxx 2012-07-31 22:37:50

+0

你是對的,我更新了我的評論。 – Sam 2012-07-31 22:45:53