在阅读json

时间:2015-07-10 06:45:11

标签: android async-onprogressupdate

这是我的代码我无法更新progressbar中的计时器:

class IPListAsy extends AsyncTask<String, Integer, Integer>{        

        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(CurrentHealthActivity.this, "Wait", "getting...");
             this.progressDialog.setIndeterminate(false);
                this.progressDialog.setMax(100);
                this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                this.progressDialog.setCancelable(false);
            super.onPreExecute();
        }

        @Override
        protected Integer doInBackground(String... params) {
            arrayList=service.getIPList(params[0], params[1]);
            System.out.println("1-result"+arrayList.size());
            return arrayList.size();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {        
             int current = values[0];
                int total = values[1];
         System.out.println("value="+values.toString());
                float percentage = 100 * (float) current / (float) total;

                progressDialog.setProgress((int) percentage);
        }

        @Override
        protected void onPostExecute(Integer result) {
            progressDialog.dismiss();

            if(result > 0){
                System.out.println("3-result"+result);
                fillItemListAdapter();
            }
            else{
                Toast.makeText(getApplicationContext(), "No Record Found", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(result);
        }
    }

1 个答案:

答案 0 :(得分:0)

您必须使用publishProgress()方法中的doInBackground()方法来调用onProgressUpdate()方法。

        @Override
        protected Integer doInBackground(String... params) {
            arrayList=service.getIPList(params[0], params[1]);
            System.out.println("1-result"+arrayList.size());
            int current = arrayList.get(0);
            int total = arrayList.get(1);
            float percentage = 100 * (float) current / (float) total;
            publishProgress((Integer)percentage);
            return wateverValueToReturn;  
        }

        @Override
        protected void onProgressUpdate(Integer... progress) { 

                progressDialog.setProgress(progress);
        }