runnable - Android update UI every 2 seconds -
i have been searching around , tried out solutions day nothing worked, decide post code here , hope can give me help, thanks!
i have fragment extends listfragment.
the list has 4 items; bottom three, set pictures , text in getview()
method in private listadapter class
. first one, want continuously change picture. have array, promotion_image_array
, of bitmap contains 3 bitmap use source first item. following code of getview()
, screenshot of got right now:
@override public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; view myview = convertview; system.out.println("in getview"); if (myview == null) { myview = inflater.inflate(r.layout.home_category_item, null); holder = new viewholder(); holder.item_image= (imageview)myview.findviewbyid(r.id.item_image); holder.res_name = (textview)myview.findviewbyid(r.id.res_name); if(position == 0){ holder.res_name.setvisibility(view.gone); promotion_image_view = holder.item_image; system.out.println("position == 0"); } else if(position == 1) { holder.res_name.settext("food"); holder.item_image.setbackgroundresource(r.drawable.restaurant_icon); system.out.println("position == 1"); } else if(position == 2) { holder.res_name.settext("shop"); holder.item_image.setbackgroundresource(r.drawable.shop_icon); system.out.println("position == 2"); } else if(position == 3) { holder.res_name.settext("groupon"); holder.item_image.setbackgroundresource(r.drawable.groupon_icon); system.out.println("position == 3"); } myview.settag(holder); } else { // view recycled, no need inflate holder = (viewholder) myview.gettag(); } return myview; } }
tried use handler , runnable continuously update image of first item in oncreate() method of fragment. have static variable promotion_image_view declared @ top of fragment , use line of code actual imageview of first item in getview() method:(from code piece above)
promotion_image_view = holder.item_image;
and tried set promotion_image_view's background in runnable's run() method, following code of runnable , handler in oncreate():
counter = 0; mhandler = new myhandler(this); runnable = new runnable() { @override public void run() { mhandler.postdelayed(runnable,3000); // repeatedly display 3 promotion images bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array; bitmapdrawable ob = new bitmapdrawable(my_resources, promotion_image_array[counter]); if(promotion_image_view != null) { promotion_image_view.setbackground(ob); system.out.println("promotion_image_view != null"); } system.out.format("in run: counter = %d%n", counter); counter++; if(counter == 3) counter = 0; } }; runnable.run();
both promotion_image_view != null
,
in run: counter = %d%n
, counter
got printed out properly(continuously printed out @ constant rate) , counter's value correct, too.(0,1,2,0,1,2,0.....). picture did not change @ all.
i appreciate help, thank you!!
******************************* after read answers --- first edit *******************************
thanks people answered, tried notifydatasetchanged() method, still not working; image not changed, printout looks correct.
the following revised code:
getview() method:
@override public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; view myview = convertview; if (myview == null) { myview = inflater.inflate(r.layout.home_category_item, null); holder = new viewholder(); holder.item_image= (imageview)myview.findviewbyid(r.id.item_image); holder.res_name = (textview)myview.findviewbyid(r.id.res_name); if(position == 0){ holder.res_name.setvisibility(view.gone); bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array; bitmapdrawable ob = new bitmapdrawable(my_resources, promotion_image_array[counter]); holder.item_image.setbackground(ob); system.out.format("in run: counter = %d%n", counter); counter++; if(counter == 3) counter = 0; system.out.println("in getview: position == 0"); } else if(position == 1) { holder.res_name.settext("food"); holder.item_image.setbackgroundresource(r.drawable.restaurant_icon); system.out.println("position == 1"); } else if(position == 2) { holder.res_name.settext("shop"); holder.item_image.setbackgroundresource(r.drawable.shop_icon); system.out.println("position == 2"); } else if(position == 3) { holder.res_name.settext("groupon"); holder.item_image.setbackgroundresource(r.drawable.groupon_icon); system.out.println("position == 3"); } myview.settag(holder); } else { // view recycled, no need inflate holder = (viewholder) myview.gettag(); } return myview; } }
handler , runnable:
counter = 0; mhandler = new myhandler(this); runnable = new runnable() { @override public void run() { mhandler.postdelayed(runnable, 3000); adapter.notifydatasetchanged(); } }; runnable.run();
and following print out looks correct me:
i/system.out﹕ in run: counter = 0
i/system.out﹕ in getview: position == 0
i/system.out﹕ position == 1
i/system.out﹕ position == 2
i/system.out﹕ position == 3
i/system.out﹕ in run: counter = 1
i/system.out﹕ in getview: position == 0
i/system.out﹕ in run: counter = 2
i/system.out﹕ in getview: position == 0
i/system.out﹕ in run: counter = 0
i/system.out﹕ in getview: position == 0
i/system.out﹕ in run: counter = 1
i/system.out﹕ in getview: position == 0
i/system.out﹕ in run: counter = 2
i/system.out﹕ in getview: position == 0
i/system.out﹕ in run: counter = 0
i/system.out﹕ in getview: position == 0
using static variable "promotion_image_view " not idea.
beacuse, android listview recycles it's own listiem. see below: https://stackoverflow.com/a/14108676/850347
instead, how use image slideshow library? https://github.com/daimajia/androidimageslider
or, can change real data (not generated data in getview) , call notifydatasetchanged() method this.
runonuithread(new runnable() { @override public void run() { notifydatasetchanged(); } });
edit1:
the point is: if (myview == null)
updating imageview, if null. so, how move code outside of null check code?
@override public view getview(int position, view convertview, viewgroup parent) { final viewholder holder; view myview = convertview; if (myview == null) { myview = inflater.inflate(r.layout.home_category_item, null); holder = new viewholder(); holder.item_image = (imageview) myview.findviewbyid(r.id.item_image); holder.res_name = (textview) myview.findviewbyid(r.id.res_name); } else { // view recycled, no need inflate holder = (viewholder) myview.gettag(); } if (position == 0) { holder.res_name.setvisibility(view.gone); bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array; bitmapdrawable ob = new bitmapdrawable(my_resources, promotion_image_array[counter]); holder.item_image.setbackground(ob); system.out.format("in run: counter = %d%n", counter); system.out.println("in getview: position == 0"); counter = counter++ % 3; } else if (position == 1) { holder.res_name.settext("food"); holder.item_image.setbackgroundresource(r.drawable.restaurant_icon); system.out.println("position == 1"); } else if (position == 2) { holder.res_name.settext("shop"); holder.item_image.setbackgroundresource(r.drawable.shop_icon); system.out.println("position == 2"); } else if (position == 3) { holder.res_name.settext("groupon"); holder.item_image.setbackgroundresource(r.drawable.groupon_icon); system.out.println("position == 3"); } myview.settag(holder); return myview; }
Comments
Post a Comment