为什么在 Volley 响应结果循环下 arrayList 总是为空?



carList 在类中声明为公共变量,日志显示值已添加到数组中,但是当我调用列表时为空,如何使用 Volley 响应解决它?

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);
}

谢谢

> @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();
}
}

这很简单,需要一个微小的线索来修复它;)

感谢您的帮助

最新更新