Farzad
Farzad

Reputation: 212

Adapters dosen't work in runOnUiThread in Android

I have a bug for managing adapters in Fragment. My scenario is : in activity there are TabLayout and ViewPager for Creating tab swipping.

Inside each fragement there is a RecyclerView and Adapter. In real we don't need to use runOnUiThread, but in these method we need runOnUiThread for managing Views .

   navinAsync.getImageGalleries(paramsMap, new RequestListener() {
        @Override
        public void onResponse(final String response) {
            hideProgressBarScreenShots();


            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String message=parseAdapterMessage(response);
                        productScreenShots = getAllAppProductScreenShots(message);
                        LinearLayoutManager layoutManager
                                = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
                        app_images.setLayoutManager(layoutManager);
                        app_images.setHasFixedSize(true);
                        ApplicationImagesRecyclerListAdapter listAdapter = new ApplicationImagesRecyclerListAdapter(
                                getActivity(), productScreenShots);
                        app_images.setAdapter(listAdapter);
                        app_images.setFocusable(true);

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




        }

        @Override
        public void onError(String response) {
            hideProgressBarScreenShots();
            Log.e("","");



        }
    });

public List<ProductScreenShot> getAllAppProductScreenShots(String data) {


    List<ProductScreenShot> productScreenShotList = new ArrayList<>();

    try {

        JSONArray array = new JSONArray(data);


        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            ProductScreenShot productScreenShot = new ProductScreenShot();


            productScreenShot.setProductPackageName(jsonObject.getString("product_packageName"));
            productScreenShot.setScreenShoturl(jsonObject.getString("imageUrl"));
            productScreenShot.setThumbnailUrl(jsonObject.getString("thumbnailURL"));

            productScreenShotList.add(productScreenShot);

        }


    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return productScreenShotList;
}

and Adapter :

public class ApplicationImagesRecyclerListAdapter extends DefaultRecyclerViewAdapter<ApplicationImagesRecyclerListAdapter.ViewHolder> {

    private List<ProductScreenShot> productScreenShots;
    private Context context;
    private ViewPager viewPager;
    public ApplicationImagesRecyclerListAdapter(Context context, List<ProductScreenShot> productScreenShots , ViewPager viewPager) {
        super(context);
        this.context = context;
        this.productScreenShots = productScreenShots;
        this.viewPager = viewPager;
    }

    public ApplicationImagesRecyclerListAdapter(Context context, List<ProductScreenShot> productScreenShots) {
        super(context);
        this.context = context;
        this.productScreenShots = productScreenShots;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.application_image, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        ProductScreenShot productScreenShot=productScreenShots.get(position);

        ImageView appImage = holder.appImage;
        Designer designer = new Designer(context);
        designer.setViewBackground(appImage, designer.getLightGraySelector(), true, BasicDesigner.RECTANGLE, 0);
        if (productScreenShot.getScreenShoturl().equals("null") || productScreenShot.getScreenShoturl() == null) {
            appImage.setImageResource(R.mipmap.ic_launcher);
        } else {
            Picasso.with(context).load(productScreenShot.getScreenShoturl()).error(R.mipmap.ic_launcher).into(appImage);
        }


    }

    @Override
    public int getItemCount() {
        return productScreenShots.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView appImage;

        public ViewHolder(View itemView) {
            super(itemView);
            appImage = (ImageView) itemView.findViewById(R.id.app_image);
        }
    }
}

these code sometimes work and sometimes not work,

what is problem in working with runOnUiThread with adapters in fragment?

Upvotes: 0

Views: 650

Answers (0)

Related Questions