我有一个微调器,我在其中添加了来自JSON的数据项
JSONArray arr = new JSONArray(strServerResponse);
JSONObject jsonObj = arr.getJSONObject(0);
ArrayList<String> tii = new ArrayList<String>();
ArrayList<String> tiiN = new ArrayList<String>();
for (int i = 0; i < arr.length(); i++) {
pojo = new Pojo();
JSONObject jobj2 = arr.getJSONObject(i);
String disc = jobj2.optString("name");
Log.e("Distttt", "" + disc);
String idd = jobj2.optString("id ");
// tii.add("Select District");
tiiN.add(idd);
tii.add(disc);
}
List<String> listTemp = tii;
Set<String> temp = new HashSet<String>(listTemp);
List<String> listTempN = tiiN;
Set<String> tempN = new HashSet<String>(listTempN);
SharedPreferences.Editor editor = getSharedPreferences("MyPref3", MODE_PRIVATE).edit();
temp.addAll(listTemp);
tempN.addAll(listTempN);
editor.putStringSet("title", temp);
editor.putStringSet("iddd", temp);
editor.commit();
每个数据都包含一个id和title。
我将这些添加到spinner中
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
nDialog.dismiss();
//////////////////////////////////
Pojo pojo;
SharedPreferences prefs = getSharedPreferences("MyPref3", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("title", null);
Set<String> setN = prefs.getStringSet("iddd", null);
List<String> titlesList = new ArrayList<String>(0);
titlesList.addAll(set);
List<String> descList = new ArrayList<String>(0);
descList.addAll(setN);
//servicces.clear();
for (int i=0;i<set.size();i++) {
pojo = new Pojo();
pojo.setSpinnerDistrict(titlesList.get(i));
pojo.setSpinnerDistrictId(descList.get(i));
service.add(pojo);
// ssservice.add(p);
}
service.clear();
ssservice.clear();
for (String p : set) {
pojo = new Pojo();
pojo.setSpinnerDistrict(p);
service.add(pojo);
ssservice.add(p);
}
ssservice.add(0, "Select District");
district
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
ssservice));
// Spinner on item click listener
district
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// ed_spinner = district.getSelectedItem().toString();
if (!(position == 0)) {
//district.getSelectedItem().get(position).getSpinnerDistrictId();
spinnerDistrictId = service.get(position).getSpinnerDistrictId();
Log.e("Spinner district id", ""+spinnerDistrictId);
new NetCheck2().execute();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return;
}
我使用setter和getter来设置这些值。现在我想要我设置的所选微调器项的id。我尝试使用
spinnerDistrictId = service.get(position).getSpinnerDistrictId();
但没有得到身份。
如何获得此ID ..请帮助我..
答案 0 :(得分:0)
在您的第一个for
循环中看到您要将数据添加到service
,然后您正在清除它
for (int i=0;i<set.size();i++) {
pojo = new Pojo();
pojo.setSpinnerDistrict(titlesList.get(i));
pojo.setSpinnerDistrictId(descList.get(i));
service.add(pojo);
// ssservice.add(p);
}
service.clear(); // here comment this line
并在您的其他for
循环中不要将数据添加到service
对象,否则会创建重复的条目......
for (String p : set) {
pojo = new Pojo();
pojo.setSpinnerDistrict(p);
service.add(pojo); // comment this line
ssservice.add(p);
}