android - How to set data to custom row TextView in ListView -
i want show data server, in custom listview
this row_category.xml custom row
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imageview1" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginleft="10dp" android:layout_margintop="5dp" android:src="@drawable/messenger_bubble_large_blue" /> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginleft="10dp" > <textview android:id="@+id/txttitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:text="large text" android:textappearance="?android:attr/textappearancelarge" /> </linearlayout> </linearlayout> my categoryadapter.java
public class categoryadapter extends arrayadapter{ private layoutinflater inflater; public categoryadapter(activity activity, arraylist<category> items){ super(activity, r.layout.row_category, items); inflater = activity.getwindow().getlayoutinflater(); } @override public view getview(int position, view convertview, viewgroup parent){ return inflater.inflate(r.layout.row_category, parent, false); } } my category.java class
public class category { private string name,url; public string getname() { return name; } public void setname(string name) { this.name = name; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } } and mainactivity listview
private listview categorylist; private categoryadapter categoryitemadapter; private intent intent; jsonarray jarray; arraylist<category> list; string uri="http://demopurpose.com/quiz/api/"; inputstream is; jsonobject json_data; int len; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_categories); list = new arraylist<category>(); categoryitemadapter = new categoryadapter(this, list); getdata(); setlistadapter(categoryitemadapter); categoryitemadapter.notifydatasetchanged(); } public void getdata(){ thread t = new thread() { public void run() { getdeals(); } }; t.start(); } public void getdeals() { string result = ""; try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(uri+"getcategories.php"); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch(exception e){ log.e("log_tag", "error in http connection "+e.tostring()); } //convert response string try{ bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); log.i("result...",result); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //parse json data try{ jarray = new jsonarray(result); //log.i("result", result); len=jarray.length(); runonuithread(new runnable() { public void run() { try{ category c = new category(); for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); c.setname(json_data.getstring("name")); list.add(c); categoryitemadapter.notifydatasetchanged(); } } catch(jsonexception je){ je.printstacktrace(); } } }); } catch(jsonexception e){ log.e("log_tag", "error parsing data "+e.tostring()); } } it generating list 3 items response
now, how can change text of each listview row.
you'll need improve arrayadapter.
currently you're not setting data textview. try following, didn't test should work.
public class categoryadapter extends arrayadapter { private layoutinflater inflater; public categoryadapter(activity activity, arraylist<category> items){ super(activity, r.layout.row_category, items); inflater = activity.getwindow().getlayoutinflater(); } @override public view getview(int position, view convertview, viewgroup parent){ viewholder viewholder; if (convertview == null) { viewholder = new viewholder(); convertview = inflater.inflate(r.layout.row_category, parent, false); viewholder.texttile = (textview) convertview.findviewbyid(r.id.txttitle); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } category category = (category) getitem(position); viewholder.texttile.settext(category.getname()); return convertview; } public void refresh(arraylist<category> items) { clear(); addall(items); notifydatasetchanged(); } private class viewholder { public textview texttile; } } change loop in getdeals method this
try { category c = new category(); for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); c.setname(json_data.getstring("name")); list.add(c); } categoryitemadapter.refresh(list); } catch(jsonexception je){ je.printstacktrace(); } note
you should consider using recyclerview. it's lot more powerful listview , give more control on animations of individual list items. can read on here if you'd like
Comments
Post a Comment