Android - Setting date to RelevantDate with an CursorAdapter -
let me give example of requirements trying fulfill. (sorry if it's kind of dumb question brain little fried right , i'm working on myself)
i have cursoradapter (w/ sqlite on backend) using listview display content. 1 of fields in list item displaying date item added listview. so...
case:
if today december 31st, 2013 , created list item display "today". on january 1st, 2014 date change "yesterday". , finally, on january 2nd, 2014 date change "12/31/2013".
what simple or elegant way of fulfilling these requirements? don't want checking whole listview dates , mean cpu. ideas on best practice of saving date appreciated!
thanks!
i think i've got working... use method in activity listview , call every 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 2 days after creation can skip checks item because // has been set 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 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)){ // if current date equal day after db_created date s.setdate_created("yesterday, "+db_time); //we can 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() }
Comments
Post a Comment