我这个代码getAllProductDetails应首先调用,但适配器首先运行,然后只发生服务调用

时间:2017-12-21 07:28:14

标签: android

我有这个代码getAllProductDetails(code)方法应首先调用,但适配器先运行,然后才发生服务调用。

我的images列表大小为0。问题出在哪里?

   code = 500;

    getAllProductDetails(code);

    displayProduct();
    getReviews(productDetails.getPCode());

    /*Horizontal Images Recycler view*/
    horizontalRecycler = (RecyclerView) findViewById(R.id.horizontal_recycler);

    horizontalRecycler.setHasFixedSize(true);
    horizontalRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
    horizontalImagesAdapter = new HorizontalImagesAdapter(this, images, Glide.with(this), new HorizontalImagesAdapter.Callback() {
        @Override
        public void onItemClick(int position) {

            imageView.setImageURI(Uri.parse(images.get(position).getImageUrl()));
            imagePos = Integer.toString(position);
        }
    });
    horizontalRecycler.setAdapter(horizontalImagesAdapter);
}

private void getAllProductDetails(int code){

    getRetrofitInterface().getSingleProductDetails(code).enqueue(new Callback<List<ProductDetails>>() {
        @SuppressLint("LongLogTag")
        @Override
        public void onResponse(Call<List<ProductDetails>> call, Response<List<ProductDetails>> response) {
            try {

                if(response.isSuccessful()){
                    List<ProductDetails> productDetailslist = response.body();
                    if (productDetailslist != null && productDetailslist.size() > 0) {
                        Log.i("PRODUCTS_LIST RESPONSE ------------>", productDetailslist.get(0).getResponseMsg());
                        ProductDetails productDetails = productDetailslist.get(0);
                        txtTitle.setText(productDetails.getpName());
                        txtPrice.setText("Rs." + productDetails.getProductPriceBeforeDiscount());
                        images.add(new Images(productDetails.getpImage1()));
                        images.add(new Images(productDetails.getpImage2()));
                        images.add(new Images(productDetails.getpImage3()));
                        images.add(new Images(productDetails.getpImage4()));

                        imageView.setImageURI(Uri.parse(productDetails.getpImage1()));                       
                        GlideImageLoader.loadImage(ProductDetailActivity.this, glide, imageView, 
                        productDetails.getpImage1());
                    }
                    else {
                        showUnknownError(null);
                    }
                } else {
                    String message = "";
                    int code;
                    try {
                        JSONObject jsonObject = new JSONObject(response.errorBody().string());
                        message = jsonObject.getString("ResponseMsg");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    code = response.raw().code();
                    showError(code, null, message);
                }

            } catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<List<ProductDetails>> call, Throwable t) {

        }
    });
}

3 个答案:

答案 0 :(得分:0)

更改列表后,您应该在notifyDataSetChanged()中致电HorizontalImagesAdapter

将列表存储在适配器中会更好。并为封装提供void setItems(List<Items>)之类的方法

答案 1 :(得分:0)

getAllProductDetails正在发出异步请求。然后使用空图像列表设置适配器。然后请求结束。您需要在onResponse中设置适配器。这样你就有了一个非空列表

答案 2 :(得分:0)

在API调用完成之前,您的适配器已经填充,这就是适配器为空的原因。而是最初将适配器附加到空列表,并在API调用的成功部分中,将适配器设置为数据。