For循环只显示一个数组,结尾但我有2个项目

时间:2015-11-11 18:02:34

标签: java android arrays json

For循环只显示其中的一个项目,但是我有2个项目。我必须添加listview吗?

这是我的JSON: 在子项目中我有2个阵列,其中显示1个数组只有2个项目

这是我的JSON: http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway

这里For循环我在子项目中有2项,但它只显示子项目数组中的最后一项:

"menu_name": "Beverages",
        "items": [
            {
                "id": 1,
                "BaseName": "Coca-Cola",
                "itemdesc": "",
                "subitems": [
                    {
                        "id": 1,
                        "SubItemdesc": "0.33L",
                        "SubItemprice": "0.90"
                    },
                    {
                        "id": 2,
                        "SubItemdesc": "1.5L",
                        "SubItemprice": "2.00"
                    }
                ]
            }

这是我的Java代码:

public class Secondlevel extends Activity {

    List<JSONParser> itemsdata = new ArrayList<JSONParser>();
    String item, ids;
    ListView sec;
    Second_adapter secondAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlist);
        sec = (ListView) findViewById(R.id.seondlst);
//        sec.setScrollContainer(false);
        secondAdapter = new Second_adapter(Secondlevel.this, itemsdata);
        sec.setAdapter(secondAdapter);
        Intent hello = getIntent();
        item = hello.getStringExtra("name");
        loaditems();
    }

    private void loaditems() {
        String Tag = "jsontag";
        String url = "http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
        JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject first = response.getJSONObject(i);
                        JSONArray getitems = first.getJSONArray("items");
                        for (int j = 0; j < getitems.length(); j++) {
                            JSONObject sitems = getitems.getJSONObject(j);
                            JSONParser parser = new JSONParser();
                            parser.setIid(sitems.getInt("id"));
                            parser.setBaseName(sitems.getString("BaseName"));
                            parser.setItemdesc(sitems.getString("itemdesc"));
                            JSONArray subitems = sitems.getJSONArray("subitems");
                            for (int l = 0; l < subitems.length(); l++) {
                                JSONObject thrid = subitems.getJSONObject(l);
                                parser.setSid(thrid.getInt("id"));
                                parser.setSubItemdesc(thrid.getString("SubItemdesc"));
                                parser.setSubItemprice(thrid.getString("SubItemprice"));

                            }

                            itemsdata.add(parser);
                        }

                        secondAdapter.notifyDataSetChanged();
                    }
                    progressDialog.hide();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("TaG", "Error: " + error);
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();

            }

        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                params.put("name", item);
                return params;
            }
        };
        AppController.getInstance().addToRequestQueue(arrayRequest);
    }
}

1 个答案:

答案 0 :(得分:0)

请改为尝试:

try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject first = response.getJSONObject(i);
                        JSONArray getitems = first.getJSONArray("items");
                        for (int j = 0; j < getitems.length(); j++) {
                            JSONObject sitems = getitems.getJSONObject(j);
                            JSONArray subitems = sitems.getJSONArray("subitems");
                            for (int l = 0; l < subitems.length(); l++) {
                                JSONObject thrid = subitems.getJSONObject(l);
                            JSONParser parser = new JSONParser();
                            parser.setIid(sitems.getInt("id"));
                            parser.setBaseName(sitems.getString("BaseName"));
                            parser.setItemdesc(sitems.getString("itemdesc"));
                                parser.setSid(thrid.getInt("id"));
                                parser.setSubItemdesc(thrid.getString("SubItemdesc"));
                                parser.setSubItemprice(thrid.getString("SubItemprice"));
                                itemsdata.add(parser);
                            }
                        }

                        secondAdapter.notifyDataSetChanged();
                    }
                    progressDialog.hide();
                }