我想在onItemSelected微调器中使用JsonObjectRequest,当set item spinner得到json但JsonObjectRequest无效并且什么也不显示时,如何在onItemSelected中使用JsonObjectRequest?请帮帮我
我的代码
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
JsonObjectRequest jsonObjectr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
int id_category = jsonObject.getInt("id_category");
int id_speaker = jsonObject.getInt("id_speaker");
String name = jsonObject.getString("name");
String image = jsonObject.getString("image");
String category_name = jsonObject.getString("category_name");
String speaker_name = jsonObject.getString("speaker_name");
Item_zakerin_any itemzakeran = new Item_zakerin_any(id, id_category, name, id_speaker, image, speaker_name, category_name);
list_zakeran_any.add(itemzakeran);
az = new adapter_zakeran_any(mcontext, list_zakeran_any);
rv.setAdapter(az);
hidepDialog();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidepDialog();
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectr);
}
答案 0 :(得分:0)
您正在循环中设置适配器。将其移出如:
try {
JSONArray jsonArray = response.getJSONArray("list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
int id_category = jsonObject.getInt("id_category");
int id_speaker = jsonObject.getInt("id_speaker");
String name = jsonObject.getString("name");
String image = jsonObject.getString("image");
String category_name = jsonObject.getString("category_name");
String speaker_name = jsonObject.getString("speaker_name");
Item_zakerin_any itemzakeran = new Item_zakerin_any(id, id_category, name, id_speaker, image, speaker_name, category_name);
list_zakeran_any.add(itemzakeran);
}
az = new adapter_zakeran_any(mcontext, list_zakeran_any);
rv.setAdapter(az);
hidepDialog();
} catch (JSONException e) {
e.printStackTrace();
hidepDialog();
}
如果对话异常,则不要忘记隐藏对话框,以免它们被永久阻止。