我有一个JSON格式的数据,包含城市ID和名称。当一个项目被选中时我想要它的cityid。我试过这样做。但它没有显示正确的cityid。
JSONArray gg = new JSONArray(objCity);
ArrayList<String> cityal = new ArrayList<String>();
ArrayList<String> cityalid = new ArrayList<String>();
for (int j=0; j<gg.length(); j++){
pojo = new Pojo();
JSONObject jobj = gg.getJSONObject(j);
String cityid = jobj.optString("id");
String cityName = jobj.optString("name");
cityal.add(cityName);
cityalid.add(cityid);
}
List<String> listTem = cityal;
List<String> listTemId = cityalid;
Set<String> tem = new HashSet<String>(listTem);
Set<String> temid = new HashSet<String>(listTemId);
SharedPreferences.Editor editr = getSharedPreferences("City", MODE_PRIVATE).edit();
tem.addAll(listTem);
temid.addAll(listTemId);
editr.putStringSet("cityname", tem);
editr.putStringSet("cityid", temid);
editr.commit();
和postExecute
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("Post exec calleld", "dfds");
nDialog.dismiss();
SharedPreferences prefs = getSharedPreferences("City", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("cityname", null);
Set<String> set11 = prefs.getStringSet("cityid", null);
List<String> titlesList = new ArrayList<String>(0);
titlesList.addAll(set);
List<String> descList = new ArrayList<String>(0);
descList.addAll(set11);
ssservice.clear();
for (String p : set) {
pojo = new Pojo();
pojo.setCityName(p);
al_city.add(pojo);
ssservice.add(p);
}
for (String p11 : set11) {
pojo = new Pojo();
pojo.setCityId(p11);
al_cityId.add(pojo);
ssservice11.add(p11);
}
Log.e("Cityname",""+al_city);
Log.e("City iddd", ""+al_cityId);
spinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
ssservice));
// Spinner on item click listener
spinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
ed_spinner = spinner.getSelectedItem().toString();
String idd = pojo.getCityId();
Log.e("City id from spinner",""+idd);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.textview, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptsView);
// set dialog message
alertDialogBuilder.setTitle("Select City");
// alertDialogBuilder.setIcon(R.drawable.ic_launcher);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
final Spinner mSpinner= (Spinner) promptsView
.findViewById(R.id.c_spinner);
//mSpinner.setAdapter(ssservice);
mSpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
ssservice));
final Button mButton = (Button) promptsView
.findViewById(R.id.c_btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos= mSpinner.getSelectedItemPosition();
Log.e("POS",""+pos);
pojo = new Pojo();
String cid = al_cityId.get(pos).getCityId();
Log.e("ciddddd",""+cid);
SharedPreferences.Editor editor = getSharedPreferences(
"CityIdSpinner", MODE_PRIVATE).edit();
editor.putString("id", cid);
}
});
// reference UI elements from my_dialog_layout in similar fashion
// mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);
return;
}
这是我的jsom
[{"city":[{"id":"3","name":"Pune"},{"id":"4","name":"Bhopal"},{"id":"5","name":"Indore"},{"id":"6","name":"Amravati"},{"id":"8","name":"Wardha"},{"id":"12","name":"Nagpur"},{"id":"16","name":"akola"}]
请建议我。
答案 0 :(得分:1)
将您的值存储在HashMap中。 cityname为Key,ID为value。
HashMap<String,String> spinnerValuesMap = new HashMap<String,String>();
for (int j=0; j<jsonArray.length(); j++){
JSONObject jobj = gg.getJSONObject(j);
String cityid = jobj.optString("id");
String cityName = jobj.optString("name")
spinnerValuesMap.put(cityName,cityid);
}
像这样的访问ID:
String selectedItem= mySpinner.getSelectedItem().toString();
String cityId = spinnerValuesMap.get(selectedItem); // Your CIty id is here