我正在尝试通过循环从ArrayList获取数据,但它显示列表为空并且arrayList.size()
为0,即使我的adapter.getItemCount()
也返回0但是我的arrayList不是空的它在至少2或3个数据,它完全放在RecyclerView中,但arrayList.size()
仍为0
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;
}
答案 0 :(得分:1)
所以这是一个愚蠢的错误。您的请求和响应是异步的。那是你的getList()
方法返回空数组,因为尚未收到响应。但由于您在回复中写了adapter.notifyDatasetChanged()
,因此您可以在RecyclerView中看到这些项目。
编辑:
除了实现之外,还有几个内存泄漏。所以我在这里更新代码。
<强> BlankFragment.java 强>
public class BlankFragment extends Fragment implements BackgroundTask.UpdateListInterface{
private Context mContext;
private List<DataProvider> arrayList;
private RecyclerView ItemRecyclerView;
private RecycleAdapter adapter;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
arrayList = new ArrayList<>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ItemRecyclerView = view.findViewById(R.id.your_rc_view);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new RecycleAdapter(arrayList,getContext());
ItemRecyclerView.setHasFixedSize(true);
ItemRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
ItemRecyclerView.setAdapter(adapter);
BackgroundTask.updateList(this);
}
@Override
public void updateList(List<DataProvider> dataProviders) {
arrayList.addAll(dataProviders);
for(int i = 0; i < arrayList.size(); i++){
int quantity = arrayList.get(i).getQuantity();
Total += quantity ;
}
adapter.notifyDatasetChanged();
}
}
<强> BackgroundTask.java 强>
public class BackgroundTask {
public static void updateList(final UpdateListInterface updateListInterface){
final List<DataProvider> arrayList = new ArrayList<>();
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);
}
//Here is how you must communicate with your calling class.
updateListInterface.updateList(arrayList);
} 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);
}
public interface UpdateListInterface{
void updateList(List<DataProvider> dataProviders);
}
}