【问题标题】:ArrayList gives null when i set in the spinner adapter?当我在微调器适配器中设置时,ArrayList 给出 null?
【发布时间】:2016-09-21 13:33:12
【问题描述】:

我有两个微调器。一个代表国家名称,另一个代表地区名称。当我单击国家微调器并选择一个国家作为示例时,我想这样做,就像我根据我的 json 选择印度一样。然后在区域微调器中,仅显示与该国家相关的区域。例如,我选择印度,然后仅显示印度区域。但在我的情况下,将显示整个区域。但我不想要它。我想这样做,它显示了那个国家的特定区域。我试图用模型类来做到这一点。但是当我得到国家时,请查看我的 onCreateOptionsMenu() 方法代码,它给出了 null。我不知道我该怎么做?谁能告诉我??

这是我的 json :-

 {
      "Status": 1,
      "StatusMessage": "Country and Country Area List",
      "data": [
        {
          "CountryID": "1",
          "CountryName": "India",
          "CountryArea": [
            {
              "CountryID": "1",
              "AreaID": "1",
              "AreaName": "Kuwait City(Capital)"
            },
            {
              "CountryID": "1",
              "AreaID": "2",
              "AreaName": " Hawally"
            },
           ]
        },
        {
          "CountryID": "2",
          "CountryName": "Dubai",
          "CountryArea": [
            {
              "CountryID": "2",
              "AreaID": "6",
              "AreaName": " Jeddah"
            },
            {
              "CountryID": "2",
              "AreaID": "7",
              "AreaName": " Riyadh"
            },
            ]
    }
  ]
}

这是我解析 json 的方法:-

public void requestDataCountry() {


        mProgressDialog.show();
        StringRequest countrylistrequest = new StringRequest(Request.Method.GET, GlobalData.COUNTRYLISTURL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            mProgressDialog.dismiss();

                            final JSONObject jObject = new JSONObject(response);


                            if (jObject.getString("Status").equals("1")) {

                                JSONArray jsonArray = jObject.getJSONArray("data");

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

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    mCountryName = jsonObject.getString("CountryName");

                                    mId = jsonObject.getString("CountryID");
                                    getCountryId.add(mId);

                                    pojoCityArea.setCountryName(mCountryName);
                                    JSONArray jsonArray1 = jsonObject.getJSONArray("CountryArea");
                                    for (int j = 0; j < jsonArray1.length(); j++) {

                                        JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
                                        areaID = jsonObject1.getString("AreaID");
                                        getAreaId.add(areaID);
                                        mAreaName = jsonObject1.getString("AreaName");
                                        mAreaList.add(mAreaName);

                                    }


                                    pojoCityArea.setCountryName(mCountryName);

                                    pojoCityArea pojocityarea = new pojoCityArea();
                                    pojoCityArea.setCountryName(mCountryName )
                                    pojoCityArea.setAreaList(mAreaList);
                                    mPojoCityAreaList.add(pojoCityArea);


                                }

                                countryAdapter.notifyDataSetChanged();

                            } else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("error:", "" + volleyError.getMessage());
                        if (volleyError.getMessage() == null)
                            requestDataCountry();


                    }
                }) {


        };

        RequestQueue countryqueue = Volley.newRequestQueue(getContext());

        countryqueue.add(countrylistrequest);

    }

这是我设置微调器的片段的 onCreateOptionsMenu() 方法(国家和地区):-

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu, menu);
        super.onCreateOptionsMenu(menu, inflater);


        this.menu = menu;
        MenuItem menuItem = menu.findItem(R.id.menu_spinner).setVisible(true);
        mCountrySpinner = (Spinner) MenuItemCompat.getActionView(menuItem);


        for (PojoCityArea pojoCityArea : mPojoCityAreaList) {
            mCountryList.add(pojoCityArea.getCountryName());

        }


        countryAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, mCountryList);

        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCountrySpinner.setAdapter(countryAdapter);


        mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                mCountrySerachId = getCountryId.get(position);
                mEditor.putString(KEY_COUNTRY_ID, mCountrySerachId);
                mEditor.commit();

                ArrayAdapter<String> areaAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_expandable_list_item_1, mPojoCityAreaList.get(position).getAreaList());
                areaAdapter.setDropDownViewResource(android.R.layout.simple_expandable_list_item_1);
                mSpinnerArea.setAdapter(areaAdapter);
                mSpinnerArea.setTitle("Choose your area");



            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

【问题讨论】:

  • @SripadRaj logcat 什么也没显示。 for (PojoCityArea pojoCityArea : mPojoCityAreaList) { mCountryList.add(pojoCityArea.getCountryName()); }在这段代码中,当我打印 mCountryList 的值时,它显示为空。

标签: android json arraylist model spinner


【解决方案1】:

你最好使用数据库来解决这个问题。 首先,您必须在一个表中插入所有国家/地区名称和国家/地区 ID,并将所有其他国家/地区与国家/地区 ID 一起插入到另一个表中,现在在选择国家/地区微调器时,您只需传递国家/地区 ID 以获取基于该国家/地区 ID 的国家/地区从数据库(从表 1 中选择国家/地区名称,其中 countryid='country_id';)。将其存储到字符串类型的数组列表中,并将该数组列表设置为微调器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多