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"}]

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting error at line ListView listView = (ListView) findViewById(R.id.lvItems); can not be resolved or is not field
what is there in simple_list_item_1?? please let me know
R.id.lvItems is the id for the listview that ur using in the xml. and R.layout.simple_list_item_1 this is the default display layout for the listview for a single row
R.id.lvItems is there in simple_list_item_1?
please check the above for the xml code also. and i do recomend you to go through the basic tutorials for android listview
0

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

1 Comment

can you please show? how to do that with in my code.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.