2016-09-19 4 views
0

jsonからデータを取得しています。アルファベット順に国名をソートしました。しかし、スピナーアイテムをクリックすると、item.canの誰かが私にスピンナーのソートアイテムの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); 

     } 

これは私のスピナーコード: -

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

      } 
     }); 


    } 

私の問題は、ドバイ(CountryName)を私のjsonとしてクリックした後、私はid1(Coun tryID)。一方、ドバイ(CountryName)をクリックすると、id2(CountryID)を取得します。私はCountryNameをソートした問題を知っていましたが、私はCountryIDをソートしませんでした。私の質問はCountryNameに従ってCountryIDをソートする方法です。

答えて

0

解析された値(IDと国名)をハッシュマップに保存します。次に、以下のコードを使用してハッシュマップをソートします。

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; 
} 
+0

@ Febi Mathew親愛なる、あなたの方法はまったく正しいです。アルファベット順にリストをソートしたいのですが、あなたのメソッドは同じIDを与えていますが、アルファベット順に国名をソートしませんでした。国名をアルファベット順に並べ替えることができます。その後、同じ国IDを返します。 – LoveAndroid

関連する問題