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

Android 在进度条上显示导入的数据,android,import,progress,Android,Import,Progress,我正在android中使用通讯簿应用程序。在我的应用程序中,我从应用程序中的电话簿导入联系人。导入时,我显示进度条。导入时,我想在进度条上显示导入的联系人。如何操作? 以下是我的代码:- public class Task extends AsyncTask<String, Integer, Void> { private ProgressDialog dialog; protected Context applicationContext;

我正在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();



        }
公共类任务扩展了异步任务{
私人对话;
受保护的上下文应用程序上下文;
@凌驾
受保护的void onPreExecute()
{
System.out.println(“预执行中”);
this.dialog=ProgressDialog.show(applicationContext,“导入联系人”,“请稍候…”,true);
设置ProgressStyle(ProgressDialog.STYLE_水平);
}
@凌驾
受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
System.out.println(“后台”);
addcontacts();//返回flag1;
//dialog.setMessage(名称);
返回null;
}
受保护的void onProgressUpdate(字符串…进度)
{
System.out.println(“更新中”);
}                      
@凌驾
受保护的void onPostExecute(未使用的void){
这个.dialog.cancel();
System.out.println(“在执行后”);
final AlertDialog.Builder alertbox1=新建AlertDialog.Builder(Import.this);
游标c=data.getData();
int num=c.getCount();
alertbox1.setMessage(+num+“已导入联系人”);
c、 close();
//设置一个肯定/是按钮并创建一个侦听器
alertbox1.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
//单击按钮时执行某些操作
公共void onClick(对话框接口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]);
 }
protectedvoiddoinbackground(字符串…参数){
对于(int i=1;i,使用dialog.setMessage(“msg”)是正确的,但请记住,您不能在

doInBackground(…)方法,因此您可以使用处理程序发布消息,也可以使用runOnUiThread。

monika希望在导入联系人时显示进度,尽管她希望显示的是正在导入的联系人的姓名,而不是到目前为止导入的百分比。她说:“我想在导入时在progressbar上显示正在导入的联系人”,她将这行代码注释为“//dialog.setMessage(name);“可以通过publishProgress将任何内容传递到UI threadCool,但当时没有对签名给予足够的关注。谢谢您的建议..但我想显示正在导入的联系人姓名。