我正在开发 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/