【问题标题】:How to get the id of the sorted item of the spinner?如何获取微调器的排序项的 id?
【发布时间】:2017-01-27 03:25:24
【问题描述】:

我正在从 json 中获取数据。我已经按字母顺序对国家名称进行了相应的排序。但是当我单击微调器项目时,我得到了该项目的磨损 id。谁能告诉我如何获得微调器的排序项目的 id。

这是我的 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");
                                        mCountryList.add(mCountryName);
                                        Collections.sort(mCountryList);
                                        getCountryId.add(mId);





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

                                            JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
                                            String countryAreaId = jsonObject1.getString("CountryID");


                                            mAreaName = jsonObject1.getString("AreaName");
                                            mAreaList.add(mAreaName);




                                        }
                                    }





                                    countryAdapter.notifyDataSetChanged();




                                } else {

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {




                        }
                    }) {


            };

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

            countryqueue.add(countrylistrequest);

        } 

这是我的 Spinner 代码:-

   @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);


        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();



            }

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

            }
        });


    }

我的问题是,当我点击微调器项目时,就像我根据我的 json 点击​​迪拜(国家名称)一样,然后我得到 id 1(国家 ID)。当我点击迪拜(国家名称)时,它应该得到 id 2(国家 ID)。我知道问题是我对 CountryName 进行了排序,但我没有对 CountryID 进行排序。那么我的问题是如何根据 CountryName 对 CountryID 进行排序。

【问题讨论】:

    标签: android json sorting spinner


    【解决方案1】:

    将您的解析值(ID 和国家/地区名称)保存在 Hashmap 中。然后使用下面的代码对 hashmap 进行排序。

    public LinkedHashMap<Integer, String> sortHashMapByValues(
            HashMap<Integer, String> passedMap) {
        List<Integer> mapKeys = new ArrayList<>(passedMap.keySet());
        List<String> mapValues = new ArrayList<>(passedMap.values());
        Collections.sort(mapValues);
        Collections.sort(mapKeys);
    
        LinkedHashMap<Integer, String> sortedMap =
            new LinkedHashMap<>();
    
        Iterator<String> valueIt = mapValues.iterator();
        while (valueIt.hasNext()) {
            String val = valueIt.next();
            Iterator<Integer> keyIt = mapKeys.iterator();
    
            while (keyIt.hasNext()) {
                Integer key = keyIt.next();
                String comp1 = passedMap.get(key);
                String comp2 = val;
    
                if (comp1.equals(comp2)) {
                    keyIt.remove();
                    sortedMap.put(key, val);
                    break;
                }
            }
        }
        return sortedMap;
    }
    

    【讨论】:

    • @Febi Mathew 亲爱的先生,您的方法绝对正确。但我想按字母顺序对列表进行排序。您的方法给了我相同的 id,但它没有按字母顺序对国家名称进行排序。所以请告诉我如何按字母顺序对国家名称进行排序,然后给出相同的国家 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多