一个列表视图中的多个数组

时间:2017-03-20 10:23:11

标签: android json listview

我是android的新手。所以请不要对这个问题投反对票。 我有一个应用程序,我必须从嵌套的json中获取数据到一个列表视图。

这是我的json代码:

{
  "status": 1,
  "data": [
    {
      "iMainServiceId": 1,
      "main": "NAILS",
      "sub": [
        {
          "iSubServiceId": 1,
          "vName": "Manicure",
          "fPrice": 15,
          "vDuration": "20 Minutes"
        },......
       ]
    }
  ]
}

如json所示.. 我想在一个列表视图中将两个数组区分为标题和内容..

例如, 我必须在列表视图内容中显示json数据,如

header value=Nails
raw value:iSubServiceId": 1,
              "vName": "Manicure",
              "fPrice": 15,
              "vDuration": "20 Minutes"

我做的是:

JSONObject json = new JSONObject(result);
                String status = json.getString("status");
                Log.d("Status:", status);

                String data = json.getString("data");
                Log.d("Data:", data);

                MainList = new ArrayList<MainArray_method>();
                JSONArray MainArray = json.getJSONArray("data");

                for (int i = 0; i < MainArray.length(); i++) {

                    JSONObject jsonObject = MainArray.getJSONObject(i);

                    MainList.add(new MainArray_method(MainArray.getJSONObject(i).getString("iMainServiceId"),
                            MainArray.getJSONObject(i).getString("main")));



                    SubList = new ArrayList<SubArray_method>();
                    JSONArray SubArray = jsonObject.getJSONArray("sub");
                    for (int j = 0; j < SubArray.length(); j++) {

                                    SubList.add(new SubArray_method(SubArray.getJSONObject(j).getString("iSubServiceId"),
                                            SubArray.getJSONObject(j).getString("vName"),
                                            SubArray.getJSONObject(j).getString("fPrice"),
                                            SubArray.getJSONObject(j).getString("vDuration")));
                        }

                    }
                       main_adapter = new MainArray_adapter(getApplicationContext(), MainList);
                        notification_header.setAdapter(main_adapter);
                       sub_adapter = new SubArray_adapter(getApplicationContext(), SubList);
                        notification_raw.setAdapter(sub_adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
}
}

我该怎么办请告诉我。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个

像这样创建一个MODEl类ModelPOJO

public class ModelPOJO
{
  String status;
  ArrayList<DataItems>data;

  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  } 

  public ArrayList<DataItems> getData() {
    return data;
  }

  public void setData(ArrayList<DataItems> data) {
    this.data = data;
  }
}

创建一个这样的MODEl类DataItems

public class DataItems 
{

    String iMainServiceId, main;
    private ArrayList<SubItems> subArrayList;

    public String getiMainServiceId() {
        return iMainServiceId;
    }

    public void setiMainServiceId(String iMainServiceId) {
        this.iMainServiceId = iMainServiceId;
    }

    public String getMain() {
        return main;
    }

    public void setMain(String main) {
        this.main = main;
    }

    public ArrayList<SubItems> getSubArrayList() {
        return subArrayList;
    }

    public void setSubArrayList(ArrayList<SubItems> subArrayList)   {
        this.subArrayList = subArrayList;
    }
 }

创建一个这样的MODEl类SubItems

class SubItems 
{
        String iSubServiceId, vName, fPrice, vDuration;

        public String getiSubServiceId() {
            return iSubServiceId;
        }

        public void setiSubServiceId(String iSubServiceId) {
            this.iSubServiceId = iSubServiceId;
        }

        public String getvName() {
            return vName;
        }

        public void setvName(String vName) {
            this.vName = vName;
        }

        public String getfPrice() {
            return fPrice;
        }

        public void setfPrice(String fPrice) {
            this.fPrice = fPrice;
        }

        public String getvDuration() {
            return vDuration;
        }

        public void setvDuration(String vDuration) {
            this.vDuration = vDuration;
        }
 }

现在就像这样阅读你的json

        ArrayList<ModelPOJO> modelPOJOArrayList;

    modelPOJOArrayList = new ArrayList<>();
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());

        ModelPOJO modelPOJO = new ModelPOJO();
        modelPOJO.setStatus(obj.getString("status"));


        ArrayList<DataItems> dataItemsArrayList = new ArrayList<>();
        ArrayList<SubItems> subItemsArrayList = new ArrayList<>();

        JSONArray dataArray = obj.getJSONArray("data");

        for (int i = 0; i < dataArray.length(); i++) {

            DataItems dataItems = new DataItems();

            JSONObject jsonObject = dataArray.getJSONObject(i);
            dataItems.setiMainServiceId(jsonObject.getString("iMainServiceId"));
            dataItems.setMain(jsonObject.getString("main"));

            JSONArray arraySub = jsonObject.getJSONArray("sub");

            for (int j = 0; j < arraySub.length(); j++) {

                SubItems subItems = new SubItems();
                JSONObject innerJsonObject = arraySub.getJSONObject(j);

                subItems.setiSubServiceId(innerJsonObject.getString("iSubServiceId"));
                subItems.setvName(innerJsonObject.getString("vName"));
                subItems.setfPrice(innerJsonObject.getString("fPrice"));
                subItems.setvDuration(innerJsonObject.getString("vDuration"));


                subItemsArrayList.add(subItems);
            }
            dataItems.setSubArrayList(subItemsArrayList);
            dataItemsArrayList.add(dataItems);

            modelPOJO.setData(dataItemsArrayList);
            modelPOJOArrayList.add(modelPOJO);


        }

        for (int i = 0; i < modelPOJOArrayList.size(); i++) {

            Log.e("STATUS :-> ", modelPOJOArrayList.get(i).getStatus());
            Log.e("getiMainServiceId :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getiMainServiceId());
            Log.e("getMain :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getMain());

            for (int j = 0; j < modelPOJOArrayList.get(i).getData().get(i).getSubArrayList().size(); j++) {
                Log.e("-----------------", "------------------");
                Log.e("getiSubServiceId :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getSubArrayList().get(j).getiSubServiceId());
                Log.e("getvName :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getSubArrayList().get(j).getvName());
                Log.e("getiSubServiceId :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getSubArrayList().get(j).getfPrice());
                Log.e("getvDuration :-> ", "" + modelPOJOArrayList.get(i).getData().get(i).getSubArrayList().get(j).getvDuration());

            }


        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

现在将此modelPOJOArrayList模型传递给您的

listview 在有任何疑问的情况下问我

答案 1 :(得分:0)

你需要在for循环中使用SubList = new ArrayList<SubArray_method>();行;