AsyncTask publishProgress not updating my Progress

2019-07-11 11:57发布

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



            }

标签: android
1条回答
何必那么认真
2楼-- · 2019-07-11 12:42

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;
查看更多
登录 后发表回答