android - Change image dynamically into a ListView from JSON -
i have following code retrivieng data url in json format , updating listview. code working perfectly. in xml layout, have 2 textview , 1 imageview.
how can update imageview dinamically?
i'm not getting images url, images stored inside project (res/drawable folder). var tag_icon has name of image exact same name of image inside project.
example:
response json: tag_icon = lamp01
name of image: lamp01.png
this main class:
public class devicelist extends listactivity { private static string url = "http://192.168.10.2/myhome/get_all_devices.php"; // hashmap listview arraylist<hashmap<string, string>> devicelist = new arraylist<hashmap<string, string>>(); // class json jsonparser jparser = new jsonparser(); // criar json nodes private static final string tag_devices = "devices"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_description = "description"; private static final string tag_icon = "icon"; // array jsonarray jsonarray devices = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.device_list); // loat listview new loaddevices().execute(); listview lv = getlistview(); } // class loaddevices class loaddevices extends asynctask<string, string, string>{ @override protected void onpreexecute(){ super.onpreexecute(); } protected string doinbackground(string... args) { // json string url jsonobject json = jparser.getjsonfromurl(url); try { // add array devices = json.getjsonarray(tag_devices); // looping trough results for(int = 0; < devices.length(); i++){ jsonobject c = devices.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_id); string name = c.getstring(tag_name); string description = c.getstring(tag_description); string icon = c.getstring(tag_icon); // creating hashmap hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_id, id); map.put(tag_name, name); map.put(tag_description, description); map.put(tag_icon, icon); devicelist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url){ runonuithread(new runnable() { public void run() { // update json listview listadapter adapter = new simpleadapter(devicelist.this, devicelist,r.layout.device_row, new string[]{ tag_name, tag_description, }, new int[] { r.id.device_row_textviewname, r.id.device_row_textviewdescription, }); // update listview setlistadapter(adapter); } }); } } }
here's xml layout single row
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/device_row_relativelayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/list_selector" android:orientation="horizontal" android:padding="5dip" > <linearlayout android:id="@+id/device_row_linearlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_marginright="5dip" android:padding="3dip" > <imageview android:id="@+id/device_row_imageviewicon" android:contentdescription="@string/app_name" android:layout_width="60dip" android:layout_height="60dip" android:src="@drawable/lamp03" /> </linearlayout> <textview android:id="@+id/device_row_textviewname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/device_row_relativelayout" android:layout_margintop="5dip" android:layout_marginleft="75dip" android:layout_torightof="@+id/device_row_imageviewicon" android:text="lâmpada quarto" android:textcolor="#4169e1" android:textsize="20dip" android:textstyle="bold" android:typeface="sans" /> <textview android:id="@+id/device_row_textviewdescription" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/device_row_textviewname" android:layout_marginbottom="5dip" android:layout_alignleft="@+id/device_row_textviewname" android:paddingtop="1dip" android:layout_marginright="15dip" android:layout_centerhorizontal="true" android:text="usado para controlar lâmpada quarto." android:textcolor="#343434" android:textsize="13dip" /> </relativelayout>
if images in drawable folder need first use title find image in drawable folder. make sure title of file same title tag returning server.
int imageid = getresources().getidentifier("yourpackagename:drawable/" + tag_title, null, null);
then find imageview , set image
imageview picture = (imageview)findviewbyid(r.id.device_row_imageviewicon); picture.setimageresource(imageid);
Comments
Post a Comment