2012-09-06 28 views
0

我試圖從用戶檢查過的所有複選框中添加所有值。此外,所有未選中的複選框都將被跳過。但是,我在每個價值後都跳過一個。我需要幫助。光標循環迭代顯示所有其他值

if(cursor.moveToFirst()) 
{ 
    do 
    { 
     if (cursor.getInt(10)>0 == false) 
     { 
      cursor.moveToNext(); 
      n += cursor.getDouble(9); 
     } 

     else n += cursor.getDouble(9); 

    } while(cursor.moveToNext()); 

} 

回答

0

一次調用cursor.moveToNext()它會去到下一行 - 你調用兩次每個環路(在你的while條款,並在do

只需刪除調用moveToNext()do,你應該準備就緒:

if(cursor.moveToFirst()) // <-- this will advance the cursor to the first row 
{ 
    do 
    { 
     if (cursor.getInt(10)>0 == false) 
     { 
      //cursor.moveToNext(); <--you already called this! 
      n += cursor.getDouble(9); 
     } 

     else n += cursor.getDouble(9); 

    } while(cursor.moveToNext()); // <-- this advances the cursor 

} 
+0

如果我刪除了'cursor.moveToNext()'我會得到所有的值的總和。我只想要檢查的值或'if(cursor.getInt(10)> 0 == true)' –

0

你正在做使用MoveToNext()太多

試試這個刪除一個循環中:

if(cursor.moveToFirst()) 
{ 
    do 
    { 
    if (cursor.getInt(10)>0 == false) 
    { 
     n += cursor.getDouble(9); 
    } 

    else n += cursor.getDouble(9); 

    } while(cursor.moveToNext()); 
} 
+0

與Sam_D相同的答案,請檢查我的回覆 –