只有一个json for循环数据会一次显示我该怎么办?

时间:2013-08-16 11:51:30

标签: android

这是我下面的json代码工作正常,但只有问题是一次循环数据显示,如果我评论
                然后JSONArray school2 = school.getJSONArray("contains"); for循环         JSONArray data2 = school.getJSONArray("nutritions");只显示其他明智的

JSONArray school2 = school.getJSONArray("contains");数据不会显示两个循环数据都会一次显示

        JSONObject json = new JSONObject(str);

        JSONObject json2 = new JSONObject(str);
        message = json2.getString("message");
        status = json2.getString("status");
        if (status.equals("1")) {

            JSONObject data = json.getJSONObject("data");

            JSONObject school = data.getJSONObject("entree");

            txt1.setText(school.getString("name"));
            // txt2.setText(school.getString("name"));
            txt3.setText(school.getString("description"));

            String url1 = school.getString("image");

            imgLoader.DisplayImage(url1, img1);

            JSONArray school2 = school.getJSONArray("contains");
            //
            for (int i = 0; i < school.length(); i++) {
                JSONObject object = school2.getJSONObject(i);


                    Category_ID.add(Long.parseLong(object.getString("id")));
                Category_name.add(object.getString("name"));
                Category_image.add(object.getString("image"));
                listview.setAdapter(cla);

            }

            JSONArray data2 = school.getJSONArray("nutritions");
            // JSONArray data2 = json.getJSONArray("contacts");
            final TableLayout table = (TableLayout) 
           findViewById(R.id.table2);
            for (int j = 0; j < data2.length(); j++) {
                final View row = createRow(data2.getJSONObject(j));
                table.addView(row);

            }

        } else {

            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        // IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public View createRow(JSONObject item) throws JSONException {
    View row = getLayoutInflater().inflate(R.layout.rows, null);
    ((TextView) row.findViewById(R.id.localTime)).setText(item
            .getString("name"));
    ((TextView) row.findViewById(R.id.apprentTemp)).setText(item

    .getString("weight"));

    return row;
}
           }



         if i comment this code then  JSONArray data2 =  
     school.getJSONArray("nutritions"); will show



    //              JSONArray school2 = 
 school.getJSONArray("contains");

 //             //
 //             for (int i = 0; i < school.length(); i++) {
 //                 JSONObject object = school2.getJSONObject(i);
 //
 //                   
   Category_ID.add(Long.parseLong(object.getString("id")));
 //                 Category_name.add(object.getString("name"));
 //                 Category_image.add(object.getString("image"));
 //                 listview.setAdapter(cla);
 //
 //             }

0 个答案:

没有答案