列表视图在解析json时没有相应地显示数据

时间:2015-04-07 09:19:24

标签: java android json android-arrayadapter

我正在解析json到list 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."
                }
            ]
        }
    ]
}

我这个数组有两个标题:毕业后毕业

但在布局中,它只显示一个值:毕业

output

1 个答案:

答案 0 :(得分:1)

以内部循环而不是外部实例化您的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();
    }
}