Android 在自定义ListView中检索json数据?
我正在用Volley库解析JSON数据,并在ListView中检索数据,但只打印最后一个值。。以下是我的实现:Android 在自定义ListView中检索json数据?,android,json,listview,android-listview,android-volley,Android,Json,Listview,Android Listview,Android Volley,我正在用Volley库解析JSON数据,并在ListView中检索数据,但只打印最后一个值。。以下是我的实现: public void getListElem(final Context context) { apws = new APWSManager(context, new APWSRequestState() { @Override public void customURLFinishedDownload(String respons
public void getListElem(final Context context) {
apws = new APWSManager(context, new APWSRequestState() {
@Override
public void customURLFinishedDownload(String response) {
// TODO Auto-generated method stub
Log.i("RESPONSE LISTVIEWPARSER : ", response);
try {
JSONObject json = null;
JSONArray array = new JSONArray(response);
for (int j = 0; j < array.length(); j++) {
json = array.getJSONObject(j);
Log.i("JSONOBJECT : ", json.toString());
parseJsonItem(json);
/* myItemList = new ItemObject(json.getString("title"),
json.getString("value_out"), json.getString("picture")); */
// m_ListItem.add(myItemList);
myList.setAdapter(myAdapter);
Log.i("LISTVIEW TITLE", array.getJSONObject(j).getString("title"));
Log.i("LISTVIEW VALUE", array.getJSONObject(j).getString("value_out"));
Log.i("LISTVIEW PICTURE", array.getJSONObject(j).getString("picture"));
}
myAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void customURLFailedDownload(String errorMessage) {
// TODO Auto-generated method stub
Log.i("ERROR REQUEST : ", errorMessage.toString());
}
});
apws.callAPWSRequest("getnews", null, null, TypeMethod.GET, true);
}
private void parseJsonItem(JSONObject item) {
try {
ItemObject itemObject = new ItemObject(item.getString("title"), item.getString("value_out"), item.getString("picture"));
// Ici à la place tu ajoutes ton objet dans ton array
m_ListItem.add(itemObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
我的ListView打印正确,但始终使用与json中最后一个值相同的值。如何检索ListView每个项目中的每个值
谢谢你们 您正在使用myAdapter设置列表适配器,但我没有看到任何代码将myAdapter设置为添加了Json值的m_ListItem?@SvenT23我定义了它,但没有显示代码:`myList=PullAndLoadListView findViewByIdR.id.refreshList;m_ListItem=新的ArrayList;myAdapter=new ListToRefreshAdaptergetApplicationContext,R.layout.list\u项,m\u ListItem`如果你检查你的m_ListItem,它会保存所有的json数据,而不仅仅是列表中显示的最后一个值?如果没有其他帮助,我会尝试为列表设置一个新的适配器,以查看notifydatasetchanged是否有问题。如myList.setAdapterNewListToRefreshAdapterGetApplicationContext、R.layout.list_项、m_ListItem;而不是notifydatasetchanged并删除for循环中当前的myList.setAdapter。