我正在构建公司事件的应用程序,我从数据库中获取事件并将其填充到ListView
适配器中,我需要在从数据库中检索数据时显示ProgressDialog
,这是我的代码`
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listlayout);
adapter = new MyArrayAdapter(this);
listView = (ListView) findViewById(R.id.list);
progressDialog = ProgressDialog.show(this, "Please wait....",
"Here your message");
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
//this is call the webservice to got filled adapter
adapter = new EventWebservice().getAdapter(this);
listView.setAdapter(adapter);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
`
发布于 2011-06-16 21:50:54
您需要阅读未同步的ws调用以及如何动态填充列表视图中的数据。下面是下面的代码片段,它可以确保无论WS CAll花费多少时间,图形用户界面都不会中断,并且流程是顺畅的:
String wsResponse[];
public void sampleFunction()
{
progressDialog = ProgressDialog.show(this, "", "Getting backup list...");
new Thread() {
public void run() {
try {
//do the ws call and store the ws response in a string array
wsResponse=wsCall();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
messageHandler.sendEmptyMessage(0);
// messageHandler.sendEmptyMessage(0);
}
}.start();
}
}
//inside the handler set the string array retrieved from the WS in sampleFunction to the adapter
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
//here write the code to assign the string array to the adapter
}
};
发布于 2011-06-16 21:50:51
我所说的是利用AsyncTask()..在preExecute()中显示ypur对话框,在postexecute()中清除;..和你放在backGround任务中的数据获取代码..我的意思是像下面这样..这是我在项目中使用的示例代码。
类背景任务扩展了AsyncTask {
@Override
protected void onPostExecute(Object result) {
dialog.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(Mwfa.this, "",
"Loading. Please wait...", true);
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... arg0) {
//your code
}
return null;
}
}
}
发布于 2011-06-16 21:55:42
我会给我们一个AsyncTask。下面是应该发生的事情的结构。
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "", "Loading. Please wait...",
true);
}
@Override
protected EventWebservice doInBackground(Void... params) {
//call the webservice and return it
}
@Override
protected void onPostExecute(EventWebservice webservice) {
adapter = webservice.getAdapter(this);;
listView.setAdapter(adapter);
dialog.dismiss();
}
https://stackoverflow.com/questions/6372797
复制相似问题