显示在列表视图机器人JSONArray数据 - 的Facebook API
我试图表示列表视图即JSONArray用户数据来自Facebook的图形API响应,显示在列表视图机器人JSONArray数据 - 的Facebook API
下面是我想要的代码:
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);
}
}
这里是JSON:
{ "data": [
{
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"likes": {
"data": [
{
"id": "xxxx",
"name": "xxx"
}
],
}
}
]
}
上面的代码变得喜欢数据,我想表现出简单的ListView这个数据。
回答:
试试这个
添加
contactList=new ArrayList<>();
SimpleAdapter适配器=新SimpleAdapter( MainActivity.this,contactList, R.layout.activity_listview,新的String [] { “ID”,“命名“},新INT [] {R.id.id, R.id.name});
试试这个代码
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();
}
以上是 显示在列表视图机器人JSONArray数据 - 的Facebook API 的全部内容, 来源链接: utcz.com/qa/265378.html