chohyunwook chohyunwook - 3 months ago 20
JSON Question

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 my 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();
} finally {

urlConnection.disconnect();
}
return result;

}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

try {
JSONArray arr = new JSONArray(result);
for (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 this setJSON is showing textView.
I want showing listView.

how to combination listview and JSON TEXT?

Answer

you are setting the result of async task to a text view inside onPostExecute method replace it with my suggestion. you need to add those results to list adapter and set that adapter to the 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);
             for (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