我今天搜索了一整天,试图找到一些示例代码或教程,如何在任务完成的同时创建一个进度循环。完成此任务的时间会相应地有所不同,并且有很多示例使用Thread.sleep(xxxx)使其循环,但效率不高。这是我想要做的,我想要加载一个ListView,它是在一个按钮被点击后使用JSON从web服务填充的。listview加载得很好,但根据大小的不同,加载大约需要5-10秒,所以我想在用户等待时显示旋转的循环。有没有人可以分享一些关于如何实现这一点的示例代码?
谢谢
发布于 2012-02-07 11:03:19
要调用的new Load().execute();
。
class Load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog progDailog = new ProgressDialog(Activity.this);
progDailog.setMessage("Loading...");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.setCancelable(true);
progDailog.show();
}
@Override
protected String doInBackground(String... aurl) {
//do something while spinning circling show
return null;
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
progDailog.dismiss();
}
}
发布于 2012-02-07 11:04:40
private class LoadAssync extends AsyncTask<String, Void, Void> {
protected void onPreExecute() {
ProgressDialog dialog;
dialog.setMessage("Loading...");
dialog.show();
}
protected Void doInBackground(final String... args) {
// you can do the code here
return null;
}
protected void onPostExecute(final Void unused) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
你可以这样调用assync
LoadAssync mAsyync=new LoadAssync();
mAsyync.execute(null);
发布于 2012-02-07 10:33:00
您可以尝试以下代码,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}
https://stackoverflow.com/questions/9170228
复制相似问题