当我使用自定义适配器和json时,java应用程序崩溃
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 in spinner
public void GetAllData() {
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
ArrayList<CityList>mylist=new ArrayList<CityList>();
JSONArray array=response.getJSONArray("data");
for(int index=0;index<array.length();index++)
{
JSONObject object=array.getJSONObject(index);
mylist.add(new CityList(object.getInt("id"),object.getString("name")));
}
CityAdapter cityAdapter=new CityAdapter(MainActivity.this,R.layout.custome_spinner,R.id.humanmed,mylist);
ServiceName.setAdapter(cityAdapter);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "bad", Toast.LENGTH_SHORT).show(); }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "sad", Toast.LENGTH_SHORT).show();
}
});
# 1 楼答案
与此相反:
使用:
与此相反:
使用:
你还必须把一个城市列为:
}
并将类CityList创建为:
}
# 2 楼答案
只有将其添加到自定义适配器微调器才能工作
@Override 公共视图getDropDownView(int位置,@Nullable View convertView,@NonNull ViewGroup父视图){ 返回getView(位置、转换视图、父级); }