showing listview JSON AsyncTask value on android -


i use listview

final arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1);  adapter.add("json result value"); adapter.add("json result value"); adapter.add("json result value");  listview listview = (listview) findviewbyid(r.id.listview); listview.setadapter(adapter); 

xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"           android:orientation="vertical"           android:layout_width="fill_parent"           android:layout_height="fill_parent"> <listview android:id="@+id/listview"           android:layout_width="fill_parent"           android:layout_height="wrap_content" /> </linearlayout> 

and json source.

private textview textview; string strdata = ""; @override protected void oncreate(bundle savedinstancestate) {    super.oncreate(savedinstancestate);    setcontentview(r.layout.activity_main);    new jsonget().execute(); } private class jsonget extends asynctask<string, void, string> {     @override      protected string doinbackground(string... urls);        httpurlconnection urlconnection = null;        string result = "";        try {            url url = new url("my server url");            urlconnection = (httpurlconnection) url.openconnection();             int code = urlconnection.getresponsecode();             if (code == 200) {                inputstream in = new bufferedinputstream(urlconnection.getinputstream());                if (in != null) {                    bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(in));                    string line = "";                     while ((line = bufferedreader.readline()) != null)                          result += line;                    }                    in.close();                      }             return result;          } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } {              urlconnection.disconnect();         }         return result;      }     @override     protected void onpostexecute(string result) {         super.onpostexecute(result);          try {              jsonarray arr = new jsonarray(result);              (int i= 0; i< arr.length(); i++) {                 jsonobeject jo = arr.getjsonobject(i);                strdata += jo.getstring("name") + jo.getstring("age");              }         } catch (jsonexception e){             log.d(tag,"ttt");         }          textview = (textview) findviewbyid(r.id.text);         textview.settext(strdata); 

but setjson showing textview. want showing listview.

how combination listview , json text?

you setting result of async task text view inside onpostexecute method replace suggestion. need add results list adapter , set adapter list.

@override     protected void onpostexecute(string result) {         super.onpostexecute(result);       final arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1);          try {              jsonarray arr = new jsonarray(result);              (int i= 0; i< arr.length(); i++) {                 jsonobeject jo = arr.getjsonobject(i);             string   strdata = jo.getstring("name") + jo.getstring("age");            adapter.add(strdata);              }         } catch (jsonexception e){             log.d(tag,"ttt");         }         // textview = (textview) findviewbyid(r.id.text);       //  textview.settext(strdata);      listview listview = (listview) findviewbyid(r.id.listview);     listview.setadapter(adapter);       } 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -