我想通過循環從ArrayList中獲取數據,但它顯示該列表爲空,並且arrayList.size()
爲0即使我的adapter.getItemCount()
也返回0但我的arrayList不是空的至少有2個或3個數據,它在RecyclerView完美放置,但仍然arrayList.size()
是0ArrayList.size返回0但arryList不爲空
arrayList = Backgroundtask.getList();
adapter = new RecycleAdapter(arrayList,getContext());
ItemRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
ItemRecyclerView.setLayoutManager(layoutManager);
ItemRecyclerView.setAdapter(adapter);
for(int i = 0; i < arrayList.size(); i++){
int quantity = arrayList.get(i).getQuantity();
Total += quantity ;
}
Log.d("ArrayList Size " , String.valueOf(arrayList.size()));
Log.d("getItemCount Size " , String.valueOf(adapter.getItemCount()));
這是我BackgroundTask:
public ArrayList<DataProvider> getList(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, jsonURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
DataProvider dataProvider = new DataProvider(jsonObject.getString("Name"),
jsonObject.getDouble("quantity"));
arrayList.add(dataProvider);
}
UserFragment.adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError){
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param = new HashMap<String,String>();
param.put("id",MainActivity.USER_ID.toString());
return param;
}
};
Singletone.getSingletone(context).addToRequest(stringRequest);
return arrayList;
}
'BackgroundTask.getList()'方法可以更新代碼? (int i = 0; i
JSONObject jsonObject = jsonArray.getJSONObject(i); DataProvider dataProvider = DataProvider(jsonObject.getString(「Name」), \t jsonObject.getDouble(「Quantity」)); \t arrayList.add(dataProvider); }' –
代碼不足以識別您的問題,在上面給出的代碼中,您的數組列表將僅爲空。請分享整個文件。 –