Q:

Showing JSONArray data in listview android - Facebook api

i'm trying to showing JSONArray user data in listview that is comes from facebook graph api response, 
Here is the code that i'm trying:
                    try
                    {
                        JSONObject resObject = response.getJSONObject();
                        JSONArray jsonArray = resObject.getJSONArray("data");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            if(jsonArray.getJSONObject(i).has("likes"))
                            {

                                JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                                JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                                for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                                    JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);

                                    String listid = jsonObjectlike.getString("id");
                                    String listname = jsonObjectlike.getString("name");
                                    HashMap<String, String> contact = new HashMap<>();
                                    contact.put("id", listid);
                                    contact.put("name", listname);
                                    contactList.add(contact);
                                }

                            }
                            ListAdapter adapter = new SimpleAdapter(
                                    MainActivity.this, contactList,
                                    R.layout.activity_listview, new String[]{"id", "name"}, new int[]{R.id.id,
                                    R.id.name});

                            simpleList.setAdapter(adapter);
                        }
                    }

and here is the JSON:
{
   "data": [
      {
         "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
         "likes": {
            "data": [
               {
                  "id": "xxxx",
                  "name": "xxx"
               }
            ],
         }
      }
   ]
}

The above code gets the likes data and i want to show this data in the simple listview.

A:

Try this

Add  contactList=new ArrayList<>();
SimpleAdapter  adapter = new SimpleAdapter(
                                MainActivity.this, contactList,
                                R.layout.activity_listview, new String[]{"id", "name"}, new int[]{R.id.id,
                                R.id.name});

Try this code
 public void GetFacebookData() {   

contactList=new ArrayList<>();

Bundle parameters = new Bundle();
parameters.putString("fields", "likes");
parameters.putString("limit", "50");
new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/me/posts",
        parameters,
        HttpMethod.GET,
        new GraphRequest.Callback() {

            public void onCompleted(GraphResponse response) {
                //Log.v("TAG", "Facebook Photos response: " + response);

                try
                {
                    JSONObject resObject = response.getJSONObject();
                    JSONArray jsonArray = resObject.getJSONArray("data");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        Log.i("ID", "-->" + jsonObject.getString("id"));

                        if(jsonArray.getJSONObject(i).has("likes"))
                        {

                            JSONObject jsonObjectlikes = jsonObject.getJSONObject("likes");

                            JSONArray jsonArrayLikeData = jsonObjectlikes.getJSONArray("data");

                            for (int j = 0; j < jsonArrayLikeData.length(); j++) {

                                JSONObject jsonObjectlike = jsonArrayLikeData.getJSONObject(j);

                                String listid = jsonObjectlike.getString("id");
                                String listname = jsonObjectlike.getString("name");
                                HashMap<String, String> contact = new HashMap<>();
                                contact.put("id", listid);
                                contact.put("name", listname);
                                contactList.add(contact);

                                Log.i("Like ID", "-->" + jsonObjectlike.getString("id"));
                                Log.i("Like Name", "-->" + jsonObjectlike.getString("name"));
                            }

                        }

                        SimpleAdapter  adapter = new SimpleAdapter(
                                MainActivity.this, contactList,
                                R.layout.activity_listview, new String[]{"id", "name"}, new int[]{R.id.id,
                                R.id.name});

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

