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

发布于 2024-11-16 03:35:52 字数 1069 浏览 4 评论 0原文

正在为公司事件构建应用程序,我从数据库获取事件并将其填充到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幼儿园老大 2024-11-23 03:35:52

我说的是利用 AsyncTask().. 在 preExecute() 中显示 ypur 对话框并在 postexecute() 中关闭;.. 以及您放入后台任务中的数据获取代码.. 我的意思是像下面这样.. 这是一个示例我在项目类 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;
    }

}

    }

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
{

    @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;
    }

}

    }
小巷里的女流氓 2024-11-23 03:35:52

我想要一个 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();
}

I would us a AsyncTask. Here is the structure of what should happen.

    @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();
}
仅冇旳回忆 2024-11-23 03:35:52

您需要阅读不同步的 ws 调用以及如何动态填充列表视图中的数据。下面是有效的代码片段,它将确保无论 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

        }
    };

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:

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

        }
    };
GRAY°灰色天空 2024-11-23 03:35:52

将您的代码

listView.setAdapter(adapter);
progressDialog.dismiss();
adapter.notifyDataSetChanged();

移入 Handler 并在 Thread.run() 中调用 Handler 的方法 sendEmptyMessage()您已获得适配器

请考虑这篇文章了解更多信息

编辑:

你的代码应该看起来像这样。

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 的答案是完成此类工作的最佳方式。

Move your

listView.setAdapter(adapter);
progressDialog.dismiss();
adapter.notifyDataSetChanged();

into a Handler and call the method sendEmptyMessage() of Handler from the Thread.run() after you got the Adapter.

Consider this post for more information

Edit:

Your code should be look like something this.

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();

Where your Handler will update the list. But devA's answer is best way to do such jobs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文