首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用数据库时,我需要有关线程和ProgressDialog的帮助

调用数据库时,我需要有关线程和ProgressDialog的帮助
EN

Stack Overflow用户
提问于 2011-06-16 21:38:58
回答 4查看 592关注 0票数 0

我正在构建公司事件的应用程序,我从数据库中获取事件并将其填充到ListView适配器中,我需要在从数据库中检索数据时显示ProgressDialog,这是我的代码`

代码语言:javascript
运行
复制
@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();

`

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-06-16 21:50:54

您需要阅读未同步的ws调用以及如何动态填充列表视图中的数据。下面是下面的代码片段,它可以确保无论WS CAll花费多少时间,图形用户界面都不会中断,并且流程是顺畅的:

代码语言:javascript
运行
复制
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

        }
    };
票数 0
EN

Stack Overflow用户

发布于 2011-06-16 21:50:51

我所说的是利用AsyncTask()..在preExecute()中显示ypur对话框,在postexecute()中清除;..和你放在backGround任务中的数据获取代码..我的意思是像下面这样..这是我在项目中使用的示例代码。

类背景任务扩展了AsyncTask {

代码语言:javascript
运行
复制
    @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
EN

Stack Overflow用户

发布于 2011-06-16 21:55:42

我会给我们一个AsyncTask。下面是应该发生的事情的结构。

代码语言:javascript
运行
复制
    @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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6372797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档