0

Trying to display the data of JSONArray in android listview.But i am not able to get it. Here is the source code

               @Override
                public void onSuccess(String response) {
                // Hide Progress Dialog
                prgDialog.hide();
                 try {
                 Log.d("sssss..", "rrrrr...");
                     // JSON Object
                     //JSONObject obj = new JSONObject(response);
                     JSONArray obj=new JSONArray(response);
                     Log.d("hello", "hiiii");
                     for (int i = 0; i < obj.length(); i++) {
                         Log.d("srikanth", "hiiii");
                            // JSONObject obj2 = obj.getJSONObject(i);
                         Log.d("TEST", obj.toString());
                     }

             } catch (JSONException e) {

                 e.printStackTrace();

             }
         }

and the following output I am getting in logcat

12-02 16:18:44.000: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.000: D/srikanth(272): hiiii
12-02 16:18:44.000: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.010: D/srikanth(272): hiiii
12-02 16:18:44.010: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.010: D/srikanth(272): hiiii
12-02 16:18:44.021: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.021: D/srikanth(272): hiiii
12-02 16:18:44.030: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.030: D/srikanth(272): hiiii
12-02 16:18:44.040: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.050: D/srikanth(272): hiiii
12-02 16:18:44.050: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.050: D/srikanth(272): hiiii
12-02 16:18:44.060: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.060: D/srikanth(272): hiiii
12-02 16:18:44.060: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.060: D/srikanth(272): hiiii
12-02 16:18:44.070: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.070: D/srikanth(272): hiiii
12-02 16:18:44.070: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]

12-02 16:18:44.070: D/srikanth(272): hiiii
12-02 16:18:44.080: D/TEST(272): [{"id":12,"num":"321"},{"id":11,"num":"clinical"},{"id":10,"num":"fsd"},{"id":9,"num":"2015"},{"id":8,"num":"133"},{"id":7,"num":"007"},{"id":6,"num":"TESTSTUDY"},{"id":5,"num":"111"},{"id":4,"num":"1000"},{"id":3,"num":"121"},{"id":2,"num":"s one"},{"id":1,"num":"Default_Study"}]
Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69
srikanthg
  • 71
  • 1
  • 8

3 Answers3

1
        package com.example.expandablelistadapter;

public class Data {
    public String id;
    public String num;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    @Override
    public String toString() {
        return "id=" + id + ", num=" + num + "";
    }


}


        try {
            List<Data> dataList= new ArrayList<>();

                JSONArray JSON = new JSONArray(response);

                for (int i = 0; i < JSON.length(); i++) {
                    JSONObject obj = JSON.getJSONObject(i);
                    Data d = new Data();
                    d.setId(obj.getString("id"));
                    d.setNum(obj.getString("num"));
                    dataList.add(d);

                }
               ArrayAdapter<Data> itemsAdapter =new ArrayAdapter<Data>(this,android.R.layout.simple_list_item_1, dataList);
               ListView listView = (ListView) findViewById(R.id.lvItems);
               listView.setAdapter(itemsAdapter);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

as above u can set the data item for the listview and u can also define ur own custom adapter also

and xml code for list view:

<?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="vertical" >

  <ListView
        android:id="@+id/lvItems"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>



</LinearLayout>
Manoj kumar
  • 450
  • 4
  • 13
0

You should implement a custom Adapter for your ListView. Take a look at this other question

Community
  • 1
  • 1
Alberto S.
  • 7,409
  • 6
  • 27
  • 46
0

Get the data, i.e. "id" & "num" from your JSON DATA using following code.

try {
    JSONArray JSON = new JSONArray(response);

    for (int i = 0; i < JSON.length(); i++) {
        JSONObject obj = JSON.getJSONObject(i);
        String id = obj.getString("id");
        String num = obj.getString("num");

        Log.e("JSON", "id : : " + id);
        Log.e("JSON", "num : : " + num);
    }

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

put this id and data in some list or array,

use a BaseAdapter to set these data to your listview.

Accept as answer, if helpful.