2013-07-05 70 views
0

我試圖改變名爲「日期」的列中的每個值。這基本上是需要循環通過專欄,並將每個條目更改爲最新的日期差異。問題是,我的代碼不斷改變一切到相同的價值。所以基本上,即使每個條目的「剩餘天數」應該是不同的,但這一切都歸結爲相同的數字。這是我的代碼。Android遊標循環遍歷欄

String[] projection = { HabitTable.COLUMN_ENDDATE, HabitTable.COLUMN_ID }; 
     //Get end date from database 
     Cursor mCursor = getContentResolver().query(MyHabitContentProvider.CONTENT_URI, projection, 
       null, null, null); 
     //Store end date in array 
     if(mCursor != null){ 
      do{ 
       endDate = mCursor.getString(mCursor.getColumnIndexOrThrow(HabitTable.COLUMN_ENDDATE)); 

       //Whole bunch of code to calculate days left 
       String[] eDate = endDate.split("-"); 
       int eDay = Integer.parseInt(eDate[0]); 
       int eMonth = Integer.parseInt(eDate[1]); 
       eMonth--; 
       int eYear = Integer.parseInt(eDate[2]); 
       Calendar cNow = Calendar.getInstance(); 
       Calendar cEnd = Calendar.getInstance(); 
       cEnd.set(Calendar.DAY_OF_MONTH, eDay); 
       cEnd.set(Calendar.MONTH, eMonth); 
       cEnd.set(Calendar.YEAR, eYear); 
       long diff = cEnd.getTimeInMillis() - cNow.getTimeInMillis(); 
       long days = diff/(24 * 60 * 60 * 1000); 
       if(days <= 0){ 
        days = 0; 
       } 

       String y = String.valueOf(days); 

       ContentValues values = new ContentValues(); 
       values.put(HabitTable.COLUMN_DAYSLEFT, y); 
       getContentResolver().update(MyHabitContentProvider.CONTENT_URI,values, null, null); 
      }while(mCursor.moveToNext()); 
     }mCursor.close(); 

我有沒有犯過很大的錯誤,我沒有發現?

回答

0

我認爲烏拉圭回合缺少的是更新

嘗試這樣

ContentValues values = new ContentValues(); 
values.put(HabitTable.COLUMN_DAYSLEFT, y); 
// Apply condition here 
String where = HabitTable.COLUMN_ID+"="+mCursor.getString(mCursor.getColumnIndexOrThrow(HabitTable.COLUMN_ID)); 
getContentResolver().update(MyHabitContentProvider.CONTENT_URI,values, where , null); 

here ID是一個很好的例子..

+0

,我認爲你是對的。我用的是相同的URI。 – user2525981

+0

你能否在你之前的編輯之前向我顯示你的答案?我只是在閱讀中,並沒有完成,我認爲這可能有助於我瞭解一點 – user2525981

+0

請點擊這裏查看http://www.anddev.org/other-coding-problems-f5/ how-to-use-getcontentresolver-update-t44066.html –