正在为公司活动构建应用程序,我从数据库获取事件并将其填入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();
`
答案 0 :(得分:1)
我所说的是使用AsyncTask()..在preExecute()中显示ypur对话框并在postexecute()中解除;以及将数据提取代码放入backGround任务中...我的意思是下面的...这个是我在项目中使用的示例代码
类Backgrountask扩展了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;
}
}
}
答案 1 :(得分:1)
我希望我们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();
}
答案 2 :(得分:0)
移动你的
listView.setAdapter(adapter);
progressDialog.dismiss();
adapter.notifyDataSetChanged();
进入Handler
并在获得sendEmptyMessage()
后从Handler
调用Thread.run()
的方法Adapter
。
考虑this post了解更多信息
修改强>
你的代码看起来应该是这样的。
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);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
您的处理程序将更新列表。但是devA's answer is best way to do such jobs.
答案 3 :(得分:0)
您需要阅读未同步的ws调用以及如何动态填充listview中的数据。下面的代码片段可以正常工作,并确保不会消耗WS CAll需要花费多少时间在GUI上没有中断并且流程顺畅:
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
}
};