2013-07-05 41 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 –