android - How to update a textview of a listItem based on the current time and the event start time -
i new android. i'm creating contest app in have listview
contains status of event. whether started or not. should updated automatically when current time exceeds event start time.
i'm using asynctask
not accurate.
@override public view getview(final int position, view view, viewgroup parent) { layoutinflater scheduleinflater = layoutinflater.from(getcontext()); final holder h; if(view == null){ view = scheduleinflater.inflate(r.layout.custom_list , parent , false); h = new holder(); h.eventname=(textview) view.findviewbyid(r.id.eventnamecustomlist); h.eventstatus=(textview) view.findviewbyid(r.id.eventstatus); h.lefttime = (textview) view.findviewbyid(r.id.lefttime); h.righttime= (textview) view.findviewbyid(r.id.righttime); view.settag(h); }else{ h = (holder) view.gettag(); } final data d = getitem(position); h.eventname.settext(d.geteventname()); date startdate= d.getstartdate(); date enddate = d.getenddate(); long sttime = startdate.gettime(); long = system.currenttimemillis(); if(now >= sttime) {h.eventstatus.settext(html.fromhtml(startedtext));} else {h.eventstatus.settext(html.fromhtml(upcomingtext));} h.eventstatus.settag(string.valueof(position)); // setting tag of each textview position avoid incorrect aynctask update new go(h.eventstatus).execute(d.getstartdate()); // starting thread update text view return view; } public class go extends asynctask<date, void , boolean> { string path; textview status; public go(textview eventstatus){ this.status=eventstatus; this.path=eventstatus.gettag().tostring(); } @override protected boolean doinbackground(date... params) { date startdate = params[0]; while(true){ long curtime = system.currenttimemillis(); long starttime = startdate.gettime(); if (curtime >= starttime) {return true;} } } @override protected void onpostexecute(boolean b) { if(!status.gettag().tostring().equals(path)) return; if(b!=null && status!=null){ if(b==true) status.settext(html.fromhtml(startedtext)); else status.settext(html.fromhtml(upcomingtext)); }else{ status.settext(html.fromhtml(upcomingtext)); } } } static class holder{ textview eventname , eventstatus; textview lefttime, righttime; }
how update textview
started when current time exceeds start time. please , suggest better solution.
the reason app becoming slow because you're needlessly running checks inside while(true)
, taking resources , making whole device slow.
better approach execute update operation after delay, when time comes.
follow these steps:
- keep time event start in sort of list/array/map (keep time relative),
use
android.os.handler
execute code delay. below (replacetimeinmillis
milliseconds after event starts):handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { adapter.notifydatasetchanged(); } }, timeinmillis);
Comments
Post a Comment