我从这个回复得到了我的微调器中的id和名字,现在我正在尝试的是如果用户从spinner中选择India然后我想获得id值,但我没有得到它,以下是我的回复和代码,任何人都可以告诉我这是什么问题..
性反应的
{"search values":
[
{"id":"1","name":"India"},
{"id":"2","name":"srilanka"}
],
"success":1
}
例如我选择india我想要结果id == 1,
当我通过身份证时,我正在获得州名单回复
{"searchvalues":[{"id":"1","name":"Tamilnadu"},{"id":"1","name":"Kerala"}],"success":1}
我的代码是:
country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
String countryname = country.getSelectedItem().toString();
StringRequest state=new StringRequest(Request.Method.POST, D_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsnobject = new JSONObject(response);
JSONArray jsonArray =jsnobject.getJSONArray("searchvalues");
distadapter.clear();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj= jsonArray.getJSONObject(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:0)
为此,您必须创建Getter-Setter类以获取1st JsonObject的Id和名称。
以下是Getter-Setter的代码
.with(this)
.load(entry.getImageUrl())
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mImageView.setImageBitmap(resource);
int width = mImageView.getMaxWidth(); //prints 0
int height = mImageView.getMaxHeight(); //prints 0
int resw = resource.getWidth(); //returns original image width
}
});
在mainActivity中创建一个列表,如下所示
public class SearchValues {
String id, name;
public SearchValues(String id, String name) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param id
* the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the id
*/
public String getId() {
return id;
}}
并在onCreate Method中声明。
List<SearchValues> values;
创建AsyncTask以获取1st JsonObjects的值
values = new ArrayList<SearchValues>;
获取这些值后,在Spinner中添加代码
@Override
protected String doInBackground(String... strings) {
try {
// get values and add these values in list like this
String id = obj.getString("id");
String name = obj.getString("name");
SearchValues val = new SearchValues(id,name);
values.add(val);}
希望这会对你有所帮助。
答案 1 :(得分:0)
你可以做一件事创建微调器的自定义视图并在微调器项中创建一个隐藏的id字段,当你选择微调器项时,从选定的微调器项中获取id
答案 2 :(得分:0)
为类中的一个创建两个ArrayList,为类中的id
创建一个,如
ArrayList data_json = new ArrayList<SearchValues>();
ArrayList country_name = new ArrayList<String>(); for name
ArrayList country_id = new ArrayList<String>(); for id
并从id
name
json
try {
jsonarray = jsonobject.getJSONArray("searchvalues");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
data_json value = new data_json();
value.setName(jsonobject.optString("name"));
value.setId(jsonobject.optString("ID"));
data_json.add(value);
country_name.add(jsonobject.optString("name"));
country_id.add(jsonobject.getString("ID"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
并在OnItemSelectedListener
ArrayAdapter adapter =
new ArrayAdapter(getApplication(), R.layout.list_item, R.id.text1,country_name);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
String cut_code = country_id.get(position);
String selectedItem = arg0.getItemAtPosition(position).toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
可能会帮助你
答案 3 :(得分:0)
这里传递选定的国家来检查json对象,如果该对象包含该城市,那么该对象应该包含与该城市相关联的id。从中获取id
ID = 0;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj= jsonArray.getJSONObject(i);
if(obj.getString("contry").equals(selected_country))
{
id=obj.getInt("id");
break;
}