Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method called before the initialization of an Adapter , executing after the adapter initialization in Android

Inside my Fragment onCreateView method I have call a method named getData() which has implemented to get some data as json response from server using android volley library.

getData() method :

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


}

Inside 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<>();)

    ...

As you can see I m calling getData() before initializing the Adapter. But CartItem_ScrollerAdapter(...) constructor is calling before the getData() method here.

Constructor of 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");
}

As you can see Log.i("executed", "Inside_onResponse"); inside the getData() method is displays in the log cat after displaying the Log.i("executed","Adapter"); which is inside CartItem_ScrollerAdapter(...) . I havent initialize it anywhere before this. So that cause my completeCartProItems ArrayList passing to the constructor of CartItem_ScrollerAdapter(...) always empty.Any suggestions to get rid of this issue would be appreciable. Thank you.

like image 457
Madushan Perera Avatar asked Jan 19 '26 07:01

Madushan Perera


1 Answers

Initialize the adapter before calling getData(). And when data is received, add it into ArrayList and call adapter's 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) {

        }
    });
like image 140
Faraz Avatar answered Jan 20 '26 22:01

Faraz