Search code examples
javaandroidgridviewandroid-progressbar

Progressbar is displaying wrong progress when using over gridview


There is a problem with the display in ProgressBar status . I have diffrent image gridview on which i want to show a progress bar status (different progress status for different images).I am just setting here if imagePosition is zero then status should be 50, on second image i want to show 90 and on third image i want to show 10 (maximum value is 100).

so the problem is its displaying 90% progress out of 100 for first and third both images.

The code is

class GridAdapter extends BaseAdapter {
    ArrayList<Topic> topicList;
    Context context;
    KarnaUtils utils;
    int width;
    KarnaContext globals;
    ArrayList<String> downloading, viewed;
    Animation not_seen;
    private ProgressBar mProgress;
      int mProgressStatus = 0;
        private Handler mHandler = new Handler();
    public GridAdapter(Context c, ArrayList<Topic> t, int w) {
        context = c;
        topicList = t;
        utils = new KarnaUtils(context);
        width = w;
        globals = (KarnaContext) context.getApplicationContext();
        downloading = globals.getDownloading_topics();
        viewed = globals.getViewedTopics();
        not_seen = AnimationUtils.loadAnimation(context, R.anim.not_seen);
    }

    @Override
    public int getCount() {
        return topicList.size();
    }

    @Override
    public Object getItem(int position) {
        return topicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    //  System.out.println("the  position is"+position);
        final View view;
        LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            view = inflator.inflate(R.layout.category_tile, null);
        } else {
            view = convertView;
        }

        Topic t = topicList.get(position);
        System.out.println("the topic id is"+t.getID());
        ImageView imageView = (ImageView) view.findViewById(R.id.category_icon);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
        imageView.setContentDescription(t.getDescription());
        imageView.setImageBitmap(utils.getLocalBitmap(t.getName() + "_" + t.getID()));

        ViewAnimator view_animator = (ViewAnimator) view.findViewById(R.id.category_icon_container);

        if (t.isClicked()) {
            if (view_animator.getCurrentView().getId() == R.id.view_one) {
                view_animator.showNext();
            }
        } else {
            if (view_animator.getCurrentView().getId() == R.id.view_two) {
                view_animator.showPrevious();
            }
        }

        boolean found = false;
        for (String str : downloading) {
            if (t.getID().equalsIgnoreCase(str)) {
                found = true;
                break;
            }
        }

        if (found) {
            ImageView progress_bar = (ImageView) view.findViewById(R.id.loading_image);
            progress_bar.setVisibility(View.VISIBLE);
            AnimationDrawable animation = (AnimationDrawable) progress_bar.getBackground();
            animation.start();
            if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 12)
                imageView.setAlpha(127);
            else
                imageView.setAlpha(0.5f);
        } else {

            boolean topic_viewed = false;
            for (String str : viewed) {
                if (t.getID().equalsIgnoreCase(str)) {
                    topic_viewed = true;
                    break;
                }
            }

            ImageView progress_bar = (ImageView) view.findViewById(R.id.loading_image);
            progress_bar.setVisibility(View.INVISIBLE);


            mProgress=(ProgressBar) view.findViewById(R.id.progressBar1);
            //  mProgress.setProgress(0);

                mProgress.setVisibility(View.VISIBLE);




            new AsyncTask<String, Void, Integer>() {
                //final int EXCEPTION = 1, NOT_REGISTERED = 2, EXISTING = 3, INVALID_ORG = 4;

                @Override
                protected void onPreExecute() {
                    //login.setEnabled(false);
                }

                @Override
                protected Integer doInBackground(String... params) {

                         incrementProgressBar(30);


                    try {
                        //user = usersdb.queryUserDomain(params[0], params[1], params[2]);
                        //stackmobLoading();

                    } catch (Exception ex) {
                        ex.printStackTrace();
                        //return EXCEPTION;
                    }

                return 0;
                }
//                protected void onProgressUpdate(int... progress) {        
//                      mProgress.setProgress(20);
//                      }

                private void incrementProgressBar(int _progress) {
                    //final int progress = _progress;
                    mHandler.post(new Runnable() {
                        public void run() {
                            //mProgress.incrementProgressBy(progress);

                            if(position==0)
                            {
                                System.out.println("The postion is"+position);

                            mProgress.setProgress(50);
                            }
                            if(position==1)
                            {
                                System.out.println("The postion is"+position);
                                mProgress.setProgress(10);
                            }
                            if(position==2)
                            {
                                System.out.println("The postion is"+position);
                                mProgress.setProgress(90);
                            }


                        }
                    });
                }


            }.execute(t.getID());



            if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 12)
                imageView.setAlpha(255);
            else
                imageView.setAlpha(1.0f);

            /*if (!topic_viewed) {
                imageView.setAnimation(not_seen);
                not_seen.start();
            } else {
                not_seen.cancel();
            }*/
        }

        return view;
    }
}

enter image description here

why the second one progress is missing. if i am adding one or more image then progressbar is being display for first and last images only.


Solution

  • Create ProgressBar object in activity scope

    And the way which you used making it as final is also fine !