我从json获取数据。我按字母顺序对国家名称进行了排序。但是当我点击微调项目时,我得到了项目的符号。任何人都告诉我如何获得微调器的已排序项目的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点击迪拜(CountryName)然后我得到id 1(CountryID)。而我点击迪拜(CountryName)它应该得到id 2(CountryID)。我知道问题我对CountryName进行了排序,但我没有对CountryID进行排序。然后我的问题是我如何根据CountryName对CountryID进行排序。
答案 0 :(得分:0)
将解析后的值(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;
}