android - 在进度条上显示导入的数据

标签 android import progress

我正在开发 android 中的地址簿应用程序。在我的应用程序中,我从我的应用程序中的电话簿导入联系人。导入时我正在显示进度条。我想在导入时在进度条上显示正在导入的联系人。如何做这? 以下是我的代码:-

public  class Task extends AsyncTask<String, Integer, Void> {
        private  ProgressDialog dialog;
        protected Context applicationContext;

        @Override
        protected void onPreExecute()
        {

            System.out.println("IN PreExecute");
            this.dialog = ProgressDialog.show(applicationContext, "Importing Contacts", "Please Wait...", true);
             dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        }
        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            System.out.println("IN BACKGROUND");
            addcontacts();//return flag1;
            //dialog.setMessage(name);
            return null ;


        }
        protected void onProgressUpdate(String... progress) 
        {
            System.out.println("IN update");

        }                      
        @Override                      
        protected void onPostExecute(Void unused) {


            this.dialog.cancel();
            System.out.println("IN PostExecute");
            final AlertDialog.Builder alertbox1=new AlertDialog.Builder(Import.this);
            Cursor c=data.getData();
            int num=c.getCount();
            alertbox1.setMessage(+num+" contacts imported");
             c.close();
            // set a positive/yes button and create a listener
            alertbox1.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1)
                {

                    call();
                }
            });
            alertbox1.show();



        }

最佳答案

onProgressUpdate 每次我们调用 publishProgress 时都会被调用,并且来自 publishProgress 的参数会转到 onProgressUpdate

所以在doInBackground()中你可以做

protected Void doInBackground(String... params) {

   for(int i=1; i<=totalContacts; ++i) {
        importNextContact();
        publishProgress(i/(float)totalContacts)*100);
   }

}

protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

关于android - 在进度条上显示导入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238725/

相关文章:

python - 无法导入姓名

python - Tkinter 进度条挂起程序并且不继续

progress-bar - Vue 和 Axios 的进度条

java - AlarmManager setRepeating()

database - 将简单图形导入 Neo4j

Python 2.7 : Unable to import matplotlib. pyplot

javascript - 如何制作进度条/容器?

java - 我该如何改进我的视频上传,目前由于内存不足导致应用程序崩溃

java - 如何处理从 onComplete 等异步方法获取的值?

java - 在调用 OnClick 之前调用方法