android - How update UI (ListView) when DB data recieved or button pressed -
i have listview represents order list, , listadapter sets recieved data db listview, , want dynamically do:
- paint white if item not selected or started
- paint green if item selected (but not started)
- paint orange if item selected started (button pressed)
- delete item if finish button pressed
*(with paint mean setbackground color of item)
**(order have atribute "state" know if started or not)
currently i'm doing when buttons pressed, , want update ui , repaint items when orders received , if 1 order started paint it
some code of ordersactivity:
private void updateorders() { if (!listitemisselected) { orderadapter.clear(); orderadapter.notifydatasetchanged(); populatelistview(); } else { debuglog("item selected, list not updated"); } } private void populatelistview() { try { // read items server log.d("lso", "reading database"); listobtainer listobtainer = new listobtainer(this); listobtainer.gettmofsdtolist(user, orderadapter); // method returns list of objects db , populates adapter } catch (exception e) { e.printstacktrace(); } configureadapter(); } private void configureadapter() { // configure listview listview = (listview) findviewbyid(r.id.lvordenes); listview.setadapter(orderadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { try { orderro orderro = (orderro) parent.getitematposition(position); listitemisselected = true; lastchildselected = currentchildselected; currentchildselected = (view) listview.getchildat(position); uptadteui(); } catch (exception e) { e.printstacktrace(); } } }); } private void uptadteui() { try { if (lastchildselected != null) { lastchildselected.setbackgroundcolor(getresources().getcolor(r.color.transparent)); } if (orderisstarted) { // block scroll, disable start, paint item disableiniciarorden(); disablebotonsalir(); enableterinarorden(); currentchildselected.setbackgroundcolor(getresources().getcolor(r.color.light_orange)); listview.setenabled(false); } else {// if there no started order listview.setenabled(true); enablebotonsalir(); disableterminarorden(); if (listitemisselected) { // enable start, paint item enableiniciarorden(); currentchildselected.setbackgroundcolor(getresources().getcolor(r.color.light_green)); } } } catch (exception e) { log.e("error", "error updating ui" + e); } } public class updateordersasync extends asynctask { @override protected object doinbackground(object[] params) { while (updateservice == true) { try { thread.sleep(180000); } catch (interruptedexception e) { e.printstacktrace(); } publishprogress(); } return null; } @override protected void onprogressupdate(object[] values) { //super.onprogressupdate(values); debuglog(". . . calling list update"); updateorders(); } }
this looks me dirty , have problem: if db changed , adapter updated can receive order started , don't know how paint activity, i'm new using custom adapters , i'm confused on should do, can paint started orders directly in adapter breaks logic did in activity(posted code) because don't know activity wich orders started(check atribute of 1 item activity).
sorry if question difficult understand or poorly explained, it's difficult me transmit problem
so, want update list of items on listview when receive more information, correct? have modified code bit enhance readability.
lets have list items :
list<items> listitems = new list<items>; listitems.add(item1); listitems.add(item2); listitems.add(item3);
with adapter associated it. associated adapter listview :
arrayadapter<string> orderadapter = new arrayadapter<string>(this,android.r.layout.listem_item, listitems); listview.setadapter(orderadapter);
now have items. lets items change :
listitems.remove(item1); // remove item1 listitems.add(item4); // , add item4
now want inform adapter have changed base data.
can :
listview.getadapter().notifydatasetchanged();
what recommend associate background color model instace of row instead of view. way, if view gets re-drawn, information still store inside model.
public class orderro ... string color = "red"; boolean isorderstarted = false; ... ... public boolean isorderstarted(){ return isorderstarted; } public void setisorderstarted(boolean isorderstarted){ this.isorderstarted = isorderstarted; } public string getcolor(){ return this.color; } public void setcolor(string color){ this.color = color; } ....
and
listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { try { orderro orderro = listview.getadapter().getitem(position); orderro.setcolor("orange"); orderro.setisorderstarted(true); uptadteui(orderro, (view) listview.getchildat(position)); } catch (exception e) { e.printstacktrace(); } } }); private void uptadteui(orderro order, view selectchild) { try { if (selectchild != null) { selectchild.setbackgroundcolor(getresources().getcolor(r.color.transparent)); } if (order.isorderstarted()) { // block scroll, disable start, paint item disableiniciarorden(); disablebotonsalir(); enableterminarorden(); selectchild.setbackgroundcolor(getresources().getcolor(r.color.light_orange)); listview.setenabled(false); } else {// if there no started order listview.setenabled(true); enablebotonsalir(); disableterminarorden(); if (listitemisselected) { // enable start, paint item enableiniciarorden(); selectchild.setbackgroundcolor(getresources().getcolor(r.color.light_green)); } } } catch (exception e) { log.e("error", "error updating ui" + e); } }
Comments
Post a Comment