java - How to refresh ImageVIew on every button click with new image from the web? -
i new android , trying create first app. should have imageview along 2 buttons (back , next). when user clicks on next button, image in imageview should replaced next image (hosted on server). names of files 1.jpg, 2.jpg, 3.jpg... using following code, not working. when activity starts first image loaded properly, when click next button nothing happens (nothing in logcat also).
public class slidesactivity extends activity { private imageview imageview; private int imagenumber = 1; private string plakatiurl = "http://plakati.bg/" + integer.tostring(imagenumber) + ".jpg"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.slides); final drawable image = loadimagefromweb(plakatiurl); imageview = new imageview(getbasecontext()); imageview = (imageview) findviewbyid(r.id.imageview1); imageview.setimagedrawable(image); button nextbutton = (button) findviewbyid(r.id.nextbtn); nextbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { imagenumber++; // have find why imageview.setimagedrawable(image); // not working here } }); private drawable loadimagefromweb(string url) { try { inputstream = (inputstream) new url(url).getcontent(); drawable d = drawable.createfromstream(is, "src name"); return d; } catch (exception e) { system.out.println("exc=" + e); return null; } } }
i know have make in different thread, don't anr, still missing here. me this, please !
this works me. can clean up, though.
public class testappactivity extends activity { private imageview imageview; private int imagenumber = 1; private string plakatiurl = "http://plakati.bg/"; private drawable image = null; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.slides); image = loadimagefromweb(generateurlstring(plakatiurl, imagenumber)); imageview = new imageview(getbasecontext()); imageview = (imageview) findviewbyid(r.id.imageview1); imageview.setimagedrawable(image); button nextbutton = (button) findviewbyid(r.id.nextbtn); nextbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { imagenumber++; image = loadimagefromweb(generateurlstring(plakatiurl, imagenumber)); imageview.setimagedrawable(image); } }); } private drawable loadimagefromweb(string url) { drawable d = null; inputstream is; try { = (inputstream) new url(url).getcontent(); d = drawable.createfromstream(is, "src name"); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return d; } private string generateurlstring(string str, int n) { return str + integer.tostring(n) + ".jpg"; } }
also, recommend using bitmap
instead of drawable
. and, i've noticed put loadimagefromweb()
method inside oncreate()
.
Comments
Post a Comment