当我调用数据库时,我需要线程和 ProgressDialog 的帮助
正在为公司事件构建应用程序,我从数据库获取事件并将其填充到 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();
`
Am building application for company events , i got the events from database and fill it in the adapter for ListView
, i need to display ProgressDialog
during the retrieving data from database , this is my code
`
@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();
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我说的是利用 AsyncTask().. 在 preExecute() 中显示 ypur 对话框并在 postexecute() 中关闭;.. 以及您放入后台任务中的数据获取代码.. 我的意思是像下面这样.. 这是一个示例我在项目类 Backgrountask 中使用的代码
扩展了 AsyncTask
{
What i say is make use of AsyncTask().. show ypur dialog in preExecute() and dismiss in postexecute();.. and the data fetching code u put in backGround task.. i mean like below.. this is a sample code i ve used in my project
class Backgrountask extends AsyncTask
{
我想要一个 AsyncTask。这是应该发生的事情的结构。
I would us a AsyncTask. Here is the structure of what should happen.
您需要阅读不同步的 ws 调用以及如何动态填充列表视图中的数据。下面是有效的代码片段,它将确保无论 WS CAll 花费多少时间,GUI 上都不会中断并且流程流畅:
You need to read on unsynchronized ws calls and how to fill up data in a listview dynamically. Here is the code snippet below that works and will ensure that no mattter how much time the WS CAll takes there is no interruption on the GUI and the flow is smooth:
将您的代码
移入
Handler
并在Thread.run()
中调用Handler
的方法sendEmptyMessage()
您已获得适配器
。请考虑这篇文章了解更多信息
编辑:
你的代码应该看起来像这样。
您的处理程序将在其中更新列表。但是 devA 的答案是完成此类工作的最佳方式。
Move your
into a
Handler
and call the methodsendEmptyMessage()
ofHandler
from theThread.run()
after you got theAdapter
.Consider this post for more information
Edit:
Your code should be look like something this.
Where your Handler will update the list. But devA's answer is best way to do such jobs.