Android 为什么在截击响应结果循环下arrayList总是空的?
carList在类中声明为公共变量,日志显示在数组中添加了值,但是当我调用列表为空时,如何使用截击响应解决它Android 为什么在截击响应结果循环下arrayList总是空的?,android,arraylist,android-volley,Android,Arraylist,Android Volley,carList在类中声明为公共变量,日志显示在数组中添加了值,但是当我调用列表为空时,如何使用截击响应解决它 public void onResponse(JSONObject response) { try { JSONArray resArray = response.getJSONArray("result"); for(int i=0;i<resArray.length();i++) { JSONObject car = resArray.getJ
public void onResponse(JSONObject response) {
try {
JSONArray resArray = response.getJSONArray("result");
for(int i=0;i<resArray.length();i++) {
JSONObject car = resArray.getJSONObject(i);
String id = car.getString("id");
String brand = car.getString("brand");
String created = car.getString("created");
carList.add(new CarListItem(brand, plate_number));
Log.d(TAG, "ADDED___: " +brand + " " + plate_number);
}
public void onResponse(JSONObject-response){
试一试{
JSONArray resArray=response.getJSONArray(“结果”);
对于(inti=0;i@Benp
如果正确,解决方案是在onResponse
主体中添加回收器适配器,如下所示:
public void onResponse(JSONObject response) {
try {
JSONArray resArray = response.getJSONArray("result");
for (int i = 0; i < resArray.length(); i++) {
JSONObject car = resArray.getJSONObject(i);
String id = car.getString("id");
String brand = car.getString("brand");
String created = car.getString("created");
carList.add(new CarListItem(car_id, brand, plateNumber, color));
}
} catch (JSONException e) {
e.printStackTrace();
}
if (carList.size() > 0) {
mAdapter = new RecyclerAdapterCarsList(carList);
buildRecyclerView();
}
}
public void onResponse(JSONObject-response){
试一试{
JSONArray resArray=response.getJSONArray(“结果”);
对于(int i=0;i0){
mAdapter=新的回收者适应车辆列表(carList);
buildRecyclerView();
}
}
这很简单,需要一点线索来解决;)
感谢您的帮助您在添加之前是否创建了实例:carList=new ArrayList();
?我怀疑您没有等待异步响应返回,因此代码执行时列表为空(因为响应尚未发生)。是的,我在onResponse
之前创建了实例,使用数组的方法在RequestQueueSingleton
方法之后。@BenP。但是调试日志显示这些值被添加到了arrayList!