0

I would like to display an progressBar in my custom adapter while downloading!

There is My Adapter Class

public class ImageAdapter extends BaseAdapter {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = ((Activity) context).getLayoutInflater().inflate(layoutResourceId, parent, false);
            holder.pictureView = (GalerieImageView) convertView.findViewById(R.id.GalerieView);
            holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBarGalerie);
            holder.progressBar.setVisibility(View.VISIBLE);
            holder.progressBar.setIndeterminate(true);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        ImageLoader imageLoader = ImageCacheManager.getInstance().getImageLoader();
        if (arrayList.get(position).getTurl() != null) {
            Log.i("lien", "" + arrayList.get(position).getTurl());
            holder.pictureView.setImageUrl(arrayList.get(position).getTurl(), imageLoader);
            holder.pictureView.setVisibility(View.VISIBLE);

            holder.pictureView.setDefaultImageResId(R.drawable.ico_loading);
            holder.pictureView.setErrorImageResId(R.drawable.ico_error);
            holder.pictureView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth, imageWidth));
            holder.pictureView
                    .setResponseObserver(new GalerieImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                            holder.progressBar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onSuccess() {
                            holder.progressBar.setVisibility(View.GONE);

                        }
                    });
        } else {
            //holder.pictureView.setVisibility(View.GONE);
        }
        return convertView;
    }
}

My Custom Networkview with Observer (GaleryImageView) it based on this StackOverFlow answer. I'm unable to display the progress bar.

Community
  • 1
  • 1
mouhcine
  • 131
  • 1
  • 3
  • 13

1 Answers1

2

I am doing exactly this. I wrapped the progress bar in a method on the calling Activity/Fragment, not the adapter itself. Something like:

public void startProgressBar() {
    dialog = new ProgressDialog(this);
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.setMessage("MEssgae");
    dialog.show();
}

Dialog is a class member.

Now when I call volley, my next call is to startProgressBar - and I cancel dialog onReponse and onError.

so it becomes

doVolleyCommand(URL);
startProgressBar();

in my response handlers:

if (dialog != null && dialog.isShowing()) { 
    dialog.dismiss();
}
  • Sorry I'm a french speaker so I would like more explication. what does mean Dialog is a class menber, should I create a class just for the ProgressBar? – mouhcine Jun 27 '14 at 15:59
  • So in my activity I declare private Dialog dialog; That way I can easily reference it without the final's etc in the respone/error response. Doing it in the adapter itself, I suspect you're attaching the progress bar to the wrong view. You really want to do this in the containing activity/fragment. I can't tell which from your code. – akameswaran Jun 27 '14 at 16:06
  • in fact i have a Gridview and I would like to display the Progressbar in each view while downloading. I have Been carefull ( I hope ) by verifing that I wasn't attaching the PB to wrong view – mouhcine Jun 27 '14 at 16:12
  • I am no expert, but when I tried doing it on my custom adapter, it didn't work and I played around with a few attempts. One thing I didn't try was using a shared context stored in the application class - which many people find a no-no. I don't want to has through singleton vs application etc, but if you are determined to do it in the adapter, I'd try that. I'm not sure the rest of the application workflow, but assuming this is triggered by the UI or a parent activity/fragment (Not the gridview either) I'd do it at that layer. – akameswaran Jun 27 '14 at 16:40
  • tkanks a lot, for your help, I will try the method you suggest me while waiting your update dude:) – mouhcine Jun 27 '14 at 17:12
  • I think the matter will be resolve if I can be informed if the custom imageView change image, do you know a method which checks if the image in a image view has change( if the default image is change by a new one?) – mouhcine Jun 27 '14 at 18:58
  • You'd have to extend imageview to do that I think. Not sure what more I can add, when I use this in my app around a volley call, although not for a image loader, I do it from the containing activity, which mostly acts like controller in a weak MVC pattern. At the moment, I got nailed by the ADT update issue and can't compile. If there's some other details I can share let me know. – akameswaran Jun 28 '14 at 12:21