在我的Fragment onCreateView
方法中,我调用了一个名为getData()
的方法,该方法已经实现了使用android volley库从服务器获取一些数据作为json响应。
getData()
方法:
public void getData(MyCustomListener<CompleteCartProItem> customListener) {
if (MyApplication.getAndroidSession().getAttribute("cart") != null) {
Log.i("cart_null", "NOT null");
RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();
CartDetails cartDetails = (CartDetails) MyApplication.getAndroidSession().getAttribute("cart");
ArrayList<CartItem> jsonSendArray = cartDetails.getShoppingList();
final String jsonString = new Gson().toJson(jsonSendArray,
new TypeToken<ArrayList<CartItem>>() {
}.getType());
Log.i("json_object", jsonString);
String url = "http://10.0.2.2:8080/ECommerceApp/getAllProductsAction";
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url,
response -> {
List<CompleteCartProItem> completeCart = new Gson().fromJson(response.toString(),
new TypeToken<List<CompleteCartProItem>>() {
}.getType());
Log.i("response", completeCart.get(0).getP_name());// successfully prints out the 0th product name.
customListener.onResponse(completeCart);
}, error -> Log.i("Volley_error", error.getMessage())) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
params.put("cartList", jsonString);
return params;
}
};
arrayRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 5000;
}
@Override
public int getCurrentRetryCount() {
return 5000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
requestQueue.add(arrayRequest);
} else {
Log.i("cart_null", "null");
}
}
内部onCreateView
:
...
getData(new MyCustomListener<CompleteCartProItem>() {
@Override
public void onResponse(List<CompleteCartProItem> response) {
completeCartProItems.addAll(response);
Log.i("executed", "Inside_onResponse");
Log.i("check", completeCartProItems.get(0).getP_name());
}
@Override
public void onError(String error_response) {
}
});
cart_item_scrollerAdapter = new CartItem_ScrollerAdapter(getActivity(), completeCartProItems);//completeCartProItems is an ArrayList of CompleteCartProItem (private List<CompleteCartProItem> completeCartProItems = new ArrayList<>();)
...
正如您所见,我在初始化适配器之前调用了getData()
。但CartItem_ScrollerAdapter(...)
构造函数在此处getData()
方法之前调用。
CartItem_ScrollerAdapter(...)
的构造函数:
public CartItem_ScrollerAdapter(Context context, List<CompleteCartProItem> completeCartProItems) {
this.inflater = LayoutInflater.from(context);
this.context = context;
this.completeCartProItems = completeCartProItems;
Log.i("executed","Adapter");
}
正如您所看到的,Log.i("executed", "Inside_onResponse");
方法中的getData()
显示在Log.i("executed","Adapter");
内的CartItem_ScrollerAdapter(...)
后显示在日志中。在此之前我没有在任何地方初始化它。所以这导致我的completeCartProItems
ArrayList传递给CartItem_ScrollerAdapter(...)
的构造函数总是空的。任何建议摆脱这个问题都是值得注意的。谢谢。
答案 0 :(得分:1)
在调用getData()
之前初始化适配器。收到数据后,将其添加到ArrayList
并调用适配器的notifyDataSetChanged();
// completeCartProItems should be initialized before passing it into adapter.
cart_item_scrollerAdapter = new CartItem_ScrollerAdapter(getActivity(), completeCartProItems);
// Set your adatper here...
cart_horizontal_scroller.setAdapter(cart_item_scrollerAdapter);
getData(new MyCustomListener<CompleteCartProItem>() {
@Override
public void onResponse(List<CompleteCartProItem> response) {
completeCartProItems.addAll(response);
cart_item_scrollerAdapter.notifyDataSetChanged();
Log.i("executed", "Inside_onResponse");
Log.i("check", completeCartProItems.get(0).getP_name());
}
@Override
public void onError(String error_response) {
}
});