java - data will not populate listview -
i'm having issues populating listview. thought had modifed xml , adapters accordingly apparently not. when activity loaded dialog pops stating it's loading app force closes. if remove 3 lines of coding stating listview.settext(name) loads items default text in xml file. code looks this:
public class displayserviceactivity extends listactivity { private listview listofservices; //jsonarrays? jsonarray directory; //json node names private static string tag_id = "id"; private static string tag_name= "name"; private static string tag_directory = "categories"; private final static string url= "api link here"; jsonobject json; jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> directorylist; @suppresslint("newapi") protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.service); directorylist = new arraylist<hashmap<string, string>>(); request request = new request(); request.execute(); listofservices = getlistview(); //get builtin listview // make sure we're running on honeycomb or higher use actionbar apis if (build.version.sdk_int >= build.version_codes.honeycomb) { // show button in action bar. getactionbar().setdisplayhomeasupenabled(true); settitle("home service categories"); } }// end of oncreate method @suppresswarnings("unused") public class request extends asynctask<string, void, jsonobject> { private static final int registration_timeout = 3 * 1000; private static final int wait_timeout = 30 * 1000; private progressdialog dialog = new progressdialog(displayserviceactivity.this); protected void onpreexecute() { dialog = new progressdialog(displayserviceactivity.this); dialog.setmessage("getting info real quick... please wait..."); dialog.show(); } protected jsonobject doinbackground(string... params) { json = jparser.getjsonfromurl(url); return json; } protected void onpostexecute(jsonobject s) { super.onpostexecute(s); string name = null; string id = null; dialog.dismiss(); try { directory = s.getjsonarray("categories"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } for(int = 0; i< directory.length(); i++){; try { id = directory.getjsonobject(i).getstring(tag_id); name = directory.getjsonobject(i).getstring(tag_name); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } displaycatlist(id, name); } } } public void displaycatlist(string id, string name){ //create new hashmap hashmap<string,string> map = new hashmap<string, string>(); //add each child node hashmap key //map.put(tag_id, id); map.put(tag_name, name); //adding hashlist arrarlist directorylist.add(map); // set adapter listview here listadapter adapter = new simpleadapter(this, directorylist, r.layout.list_item, new string[] { name }, new int[] { android.r.id.text1}); layoutinflater inflator = (layoutinflater) getsystemservice(context.layout_inflater_service); view v = inflator.inflate(r.layout.list_item, null); textview listname = (textview) v.findviewbyid(r.id.listname); listname.settext(name); setlistadapter(adapter); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); }
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <!-- single list item design --> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listname" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="rubble rubble rubble" android:textcolor="#ffffff" android:padding="10dip" android:textsize="20dip" android:textstyle="bold" > </textview>
services.xml
<?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:background="@drawable/gradient" android:orientation="vertical" > <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> <textview android:id="@+id/listname" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" android:text="rubble rubble rubble" android:textcolor="#ffffff" android:textsize="20dip" android:textstyle="bold" > </textview> </linearlayout>
// change display methods
public void displaycatlist(string id, string name){ //create new hashmap hashmap<string,string> map = new hashmap<string, string>(); //add each child node hashmap key //map.put(tag_id, id); map.put(tag_name, name); //adding hashlist arrarlist directorylist.add(map); mysimpleadapter adapter = new mysimpleadapter(this, r.layout.list_item, android.r.id.text1, directorylist); setlistadapter(adapter); adapter.notifydatasetchanged(); }
// add folloowing class in activity
public class mysimpleadapter extends arrayadapter<hashmap<string, string>> { list<hashmap<string, string>> listitems; public mysimpleadapter(context context, int resource, int textviewresourceid, list<hashmap<string, string>> objects) { super(context, resource, textviewresourceid, objects); listitems = objects; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub if(convertview == null) { layoutinflater inflator = (layoutinflater) getsystemservice(context.layout_inflater_service); convertview = inflator.inflate(r.layout.list_item, null); } textview listname = (textview) convertview.findviewbyid(r.id.listname); listname.settext(listitems.get(position).get(tag_name)); return convertview; } }
Comments
Post a Comment