使用over gridview时,Progressbar显示错误的进度

时间:2013-11-27 08:56:22

标签: java android gridview android-progressbar

ProgressBar状态显示有问题。我有不同的图像gridview我要显示进度条状态(不同图像的不同进度状态)。我只是设置在这里如果imagePosition为零然后状态应该是50,在第二个图像上我要显示90和第三个想要显示10的图像(最大值为100)。

所以问题是它在第一张和第三张图片中显示了90%的进度。

代码是

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

为什么缺少第二个进展。如果我要添加一个或多个图像,则仅显示第一张和最后一张图像的进度条。

2 个答案:

答案 0 :(得分:2)

我找到了它的解决方案。我刚刚使用了为每个位置创建的最终对象。(final ProgressBar = new ProgressBar();)。之前它是在活动开始时创建的,因此对象无法一次保存多个值。所以它只持有一个值(在任何情况下都是最后一个)。但是当我在getView中创建对象时,每次为每个将设置值的位置创建一个最终对象。它对我来说很好..但是非常感谢你们帮助我们。如果答案对你们来说似乎也很满意,请投票。

答案 1 :(得分:1)

在活动范围

中创建ProgressBar对象

你用它作为最终的方式也很好!