【问题标题】:Progressbar is displaying wrong progress when using over gridview使用网格视图时进度条显示错误的进度
【发布时间】:2013-11-27 08:56:21
【问题描述】:

ProgressBar 状态显示有问题。我有不同的图像网格视图,我想在其上显示进度条状态(不同图像的不同进度状态)。我只是在此处设置如果 imagePosition 为零,则状态应为 50,在第二张图像上我想显示 90 和第三张图片我想显示 10(最大值为 100)。

所以问题是它在第一张和第三张图片中显示 90% 的进度(满分 100 分)。

代码是

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;
    }
}

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

【问题讨论】:

  • ant erros while this process ?in logcat
  • 不,没有错误..这对我来说也有点奇怪。它是如何只为所有设置的最后一个设置值..
  • 好的,你尝试登录那里的所有值吗?

标签: java android gridview android-progressbar


【解决方案1】:

我找到了解决方案。我只是使用为每个位置创建的最终对象。(final ProgressBar = new ProgressBar();)。早些时候它是在活动开始时创建的,因此对象不能一次保存多个值。所以它只持有一个值(无论如何都是最后一个)。但是当我在 getView 中创建对象时,它每次都会为每个设置值的位置创建一个最终对象。对我来说效果很好..但非常感谢你们试图帮助我。如果你的答案也让你满意,请投票。

【讨论】:

    【解决方案2】:

    在活动范围内创建 ProgressBar 对象

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多