2013-08-24 38 views
0

讓我舉一個我試圖實現的要求示例。 (對不起,如果這是一個愚蠢的問題,但我的大腦現在有點炒,我正在自己工作)Android - 使用CursorAdapter將相關日期設置爲日期

我有一個CursorAdapter(w/SQLite在後端),我用我的ListView顯示內容。我正在顯示的列表項中的一個字段是項目添加到ListView的日期。所以......

CASE:

如果今天是2013年12月31日,我剛剛創建了一個列表項我想它顯示「今天」。 2014年1月1日,我希望日期更改爲「昨天」。最後,在2014年1月2日,我希望將日期更改爲「12/31/2013」。

滿足這些要求的簡單或最優雅的方式是什麼?我不想經常檢查我的整個列表視圖的日期和對CPU的意思。任何關於保存日期的最佳做法的想法也將非常感激!

謝謝!

回答

0

我想我已經得到它的工作......我在我的listview是活動的地方使用這種方法,並調用它每onCreate,onStart和onResume。

public void setRelevantDate() { 
    SimpleDateFormat year = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
    Date date = new Date(); 
    String current_date = year.format(date); 
    String db_day_after_creation_date; 



    //check dates created, sub "today" or "yesterday" 
    for(SoundData s: app.unsent_recordings){ 
     Log.e("soundData date flag",String.valueOf(s.isAfter_Day_two())); 
     Log.e("soundData date buffer",s.getDate_buffer()); 
     Log.e("soundData date created",s.getDate_created()); 

     if (!s.isAfter_Day_two()){ //if we are two days after the creation then we can skip all the checks for this item because 
            // it has already been set back to the initial MM/dd/yyyy date format 
     String sql_date = s.getDate_buffer(); 
     String db_date = sql_date.substring(0,10); 
     String db_time = sql_date.substring(11); 

     //find the day after creation date 
     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
     Calendar c = Calendar.getInstance(); 
     try { 
      c.setTime(sdf.parse(db_date)); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     c.add(Calendar.DATE, 1); 
     db_day_after_creation_date = sdf.format(c.getTime()); 


     if (db_date.equals(current_date)){ 
        s.setDate_created("Today, "+db_time); 
        app.update_Recording_DateCreated(s.getId(),"Today, "+db_time, s.getDate_buffer(), String.valueOf(s.isAfter_Day_two())); //dateCreated = today, TIME 
     } 
     else if(current_date.equals(db_day_after_creation_date)){      // basically if the current date is equal to the day after the db_created date 
        s.setDate_created("Yesterday, "+db_time); 
                   //we can say that it was created yesterday 
        app.update_Recording_DateCreated(s.getId(),"Yesterday, "+db_time, s.getDate_buffer(), String.valueOf(s.isAfter_Day_two())); 

     } 
     else{ 
       s.setDate_created(sql_date); 
                 //Otherwise use normal date 
       app.update_Recording_DateCreated(s.getId(),sql_date,s.getDate_buffer(), String.valueOf(s.isAfter_Day_two())); //dateCreated = dateBuffer 

     } 

      app.mCursor = app.db.getCursor(); 
      app.rec_adapter.changeCursor(app.mCursor); 
      app.rec_adapter.notifyDataSetChanged();            //Update cursor, notifyDataSetChanged() 
     } 
相關問題