我正在将 json 解析为 ListView 。数据解析得很好,但是数组适配器类中存在一些问题,它没有相应地显示 View 。
public void parseJson(JSONObject json) {
try {
JSONArray posts = json.getJSONArray("resume");
feedList = new ArrayList<JsonItem>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = (JSONObject) posts.getJSONObject(i);
JsonItem item = new JsonItem();
JSONArray cast = post.getJSONArray("Education");
Log.i("lenght", Integer.toString(cast.length()));
if (cast != null && cast.length()>0) {
for(int j =0 ;j<cast.length();j++){
JSONObject casts = (JSONObject)cast.getJSONObject(j);
item.setTitle(casts.getString("Title"));
feedList.add(item);
Log.i("feedsize", feedList.get(j).toString());
}
}
}
}catch(JSONException e){
e.printStackTrace();
}
Json数组:
{
"resume":[
{
"Education":[
{
"Title":"Post Graduation",
"Degree":"M. Tech."
},
{
"Title":"Graduation",
"Degree":"B. Tech."
}
]
}
]
}
我这个数组有两个标题:Post Graduation , Graduation
但在布局中它只显示一个值:Graduation
最佳答案
以这种方式在内部循环而不是外部循环中实例化您的 JsonItem 对象:
public void parseJson(JSONObject json) {
try {
JSONArray posts = json.getJSONArray("resume");
feedList = new ArrayList<JsonItem>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = (JSONObject) posts.getJSONObject(i);
JSONArray cast = post.getJSONArray("Education");
Log.i("lenght", Integer.toString(cast.length()));
if (cast != null && cast.length()>0) {
for(int j =0 ;j<cast.length();j++){
JsonItem item = new JsonItem();
JSONObject casts = (JSONObject)cast.getJSONObject(j);
item.setTitle(casts.getString("Title"));
feedList.add(item);
Log.i("feedsize", feedList.get(j).toString());
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
关于java - ListView 在解析 json 时没有相应地显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29488046/