AsyncTask的publishProgress没有更新我的ProgressDialog [英] AsyncTask publishProgress not updating my ProgressDialog

查看:127
本文介绍了AsyncTask的publishProgress没有更新我的ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实际任务得到完成。然而,我没有得到我的进度条的进展状态,即使我所说的进度对话框的增加..?谁能说得清我做错了?

  A级扩展的AsyncTask<文件[],整型,太虚>
                {
                    私人语境CNT;
                        诠释计数= 0;
                    @覆盖
                    在preExecute保护无效(){
                         PD =新ProgressDialog(CNT);
                            pd.setMessage(匹配的进展);
                            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                            pd.setMax(100);
                            pd.setCancelable(假);
                            pd.setProgress(0);
                            pd.show();
                    }
                    A(上下文的背景下)
                    {
                        CNT =背景;
                    }                    保护无效doInBackground(文件[] ... PARAMS){
                        // TODO自动生成方法存根
                        文件[] newfiles =参数[0];
                        文件[] TEMP = NULL;
                        INT进度= 0;
                        INT dircnt = 0;
                        INT numberofdir = newfiles.length;
                        对于(文件B:newfiles)
                        {
                            计数= 0;
                            目录名= b.getName();
                            如果(b.isDirectory())
                            {                                TEMP = b.listFiles();
                            }
                            对于(文件:TEMP)
                            {
                                GEST = dotask.batchprocess(a.getPath());
                                如果(武功!= NULL)
                                    的System.out.println(成功);
                                字符串名称=目录名称+_+计数;
                                saveGesture(姓名,武功);
                                算上++;                            }
                            dircnt ++;
                             进度=(INT)((dircnt / numberofdir)* 100);
                            publishProgress(进度);
                        }
                        返回null;
                    }
                    @覆盖
                    保护无效onProgressUpdate(整数...值){
                        //按进度增值进度条
                        pd.setProgress(值[0]);
                }
                     保护无效onPostExecute(){
                        pd.hide();
                        PD = NULL;
                     }
    }

下面是我称之为AsyncClass方法。

 公共无效addgestures()
    {
                cView.postInvalidate();
                dotask.setH(cView.getH());
                dotask.setW(cView.getW());                计数= 0;
                文件= NULL;
                / *如果(dir.isDirectory())
                {
                    文件= dir.listFiles();
                } * /
                文件= dir.listFiles();
                的for(int i = 0; I< files.length;我++)
                {
                    的System.out.println(文件[I] .getPath());
                }
                A中新= A(本);
                a.execute(文件);            }


解决方案

您整数运算坏了。行

 进度=(INT)((dircnt / numberofdir)* 100);

总是产生 0 ,因为整数除法截断只是整数部分。

要解决这个问题,只需使用漂浮来代替。具体做法是:

 浮动dircnt = 0;
浮numberofdir = newfiles.length;

My actual task is getting completed however I am not getting any increase in the progress status of my progress bar even if i call the progress dialog..?can anyone say where I did wrong?

class A extends AsyncTask<File[],Integer,Void>
                {
                    private Context cnt;
                        int count=0;
                    @Override
                    protected void onPreExecute() {
                         pd = new ProgressDialog(cnt);
                            pd.setMessage("Matching progress");
                            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                            pd.setMax(100);
                            pd.setCancelable(false);
                            pd.setProgress(0);
                            pd.show();
                    }
                    A(Context context)
                    {
                        cnt=context;
                    }

                    protected Void doInBackground(File[]... params) {
                        // TODO Auto-generated method stub
                        File[] newfiles=params[0];
                        File[] temp=null;
                        int progress=0;
                        int dircnt=0;
                        int numberofdir=newfiles.length;
                        for(File b:newfiles)
                        {


                            count=0;
                            dirname=b.getName();
                            if(b.isDirectory())
                            {

                                temp=b.listFiles();
                            }
                            for(File a:temp)
                            {
                                gest=dotask.batchprocess(a.getPath());
                                if(gest!=null)
                                    System.out.println("success");
                                String name=dirname+"_"+count;
                                saveGesture(name, gest);
                                count++;

                            }
                            dircnt++;
                             progress=(int)((dircnt/numberofdir)*100);
                            publishProgress(progress);


                        }


                        return null;
                    }
                    @Override
                    protected void onProgressUpdate(Integer... values) {
                        // increment progress bar by progress value
                        pd.setProgress(values[0]);


                }
                     protected void onPostExecute() {
                        pd.hide();
                        pd=null;
                     }
    }

below is the method from which I call the AsyncClass.

 public void addgestures()
    {
                cView.postInvalidate();
                dotask.setH(cView.getH());
                dotask.setW(cView.getW());

                count=0;
                files=null;
                /*if(dir.isDirectory()) 
                {
                    files=dir.listFiles();
                }*/
                files=dir.listFiles();
                for(int i=0;i<files.length;i++)
                {
                    System.out.println(files[i].getPath());
                }




                A a=new A(this);
                a.execute(files);



            }

解决方案

Your integer math is broken. The line

progress=(int)((dircnt/numberofdir)*100);

will always yield 0 because integer division truncates to just the integer portion.

To fix it, just use floats instead. Specifically:

float dircnt=0;
float numberofdir=newfiles.length;

这篇关于AsyncTask的publishProgress没有更新我的ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆