本文介绍了在单独的线程中填充的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我行加载一个ListView这需要很多时间的活动,所以我把这个任务在一个单独的线程,以便显示progressDialog。

In an activity I load rows of a listview which takes much time, therefore I put this task in a separate thread to allow displaying a progressDialog.

我做了以下

private void doMyStuff() {
    listItems.clear();
    progressDialog.show();

    new Thread(new Runnable() {
        @Override
        public void run() {
            for () {
                listItems.add(something to add);
            }
        handler.sendEmptyMessage(0);
        progressDialog.dismiss();
        }
    }).start();
}

private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == 0) {
            adapter.notifyDataSetChanged();
        }
    };
};

我有时这引起了一个IllegalStateException的错误。首先我很惊讶,因为线程编程一样,这就是我通常在标准Java程序。

I have sometimes a bug which raises an IllegalStateException. First of all I was surprised, because programming thread like that is what I usually do in standard Java programs.

该错误只出现有时,当逐步调试做一步它不会出现。

The bug appears only "sometimes" and it does not appear when doing step by step debugging.

这导致我在网上搜索周围,我发现SO相关的一些问题,这一点,我必须承认,事情并不清楚,我的脑海里。

This lead me to search around the web and I found some questions in SO related to this and I must admit that things are not clear to my mind.

正如我所说的notifyDataSetChanged()只有当线程完成了为什么它有时抛出一个例外。

As I call the notifyDataSetChanged() only when the thread finished why does it sometimes raises an exception.

有人可以证实我说,这样做是错误的,我必须使用异步任务,或许可以解释我为什么???

Can someone confirm me that this way of doing is wrong, and that I MUST use async task and perhaps explain me why ???

我需要有一个progressDialog显示,能有人给我的AsyncTask的填充ListView和显示的填充进度progressDialog一个简单的例子。

I need to have a progressDialog displayed, can someone give me a simple example of AsyncTask populating a listview AND displaying a progressDialog of the populating progress.

感谢

更新

jtanveer给我的答案AsyncTask的问题。现在,其他人指出,解雇是不是在处理程序,这是我纠正。

jtanveer gave me the answer to the asynctask question. Now the others pointed out that the dismiss is not in the handler, which I correct.

据jtanveer的无痛线程给出的文章,他们说,

According to the article given by jtanveer on "Painless Threading" they say that

Android提供了多种方式来访问其他线程UI线程这其中之一就是处理器。

Android offers several ways to access the UI thread from other threads which one of them is HANDLER.

是否有人知道为什么把解雇的处理程序并没有解决我的问题?对我来说listItem.add无关与用户界面?难道我错了这一点?

Does someone know why putting the dismissed in the handler did not solve my problem ? for me listItem.add has nothing to do with UI ? Am I wrong on that point ?

对于我来说,C中的唯一UI我的$ C $是适配器和progressdialog?任何评论或解释是值得欢迎的。

For me, in my code the only UI is adapter and progressdialog ? Any commentary or explanation is welcome.

最终答案

stjom给我具体的code工作答案。运行在处理器的runOnUiThread。它的工作,但我很惊讶,因为我以为处理器是在UI线程中运行...

stjom gave a working answer for my specific code. Running the runOnUiThread in the handler. It's working but I am surprised because I thought the handler was run in the Ui Thread ...

感谢名单的所有所有的答案。

Thanx to all for all your answers.

推荐答案

当你调用 adapter.notifyDataSetChanged(); 它确定了任何更改 listItems中的对象。如果任何变化被发现,它会相应地更新UI,我认为原因你的问题。您可以致电

whenever you call adapter.notifyDataSetChanged(); it identifies any changes to your listItems object. if any change is found, it will update the UI accordingly which I think causes your problem. you can call

runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

您的处理器中。

inside your handler.

这篇关于在单独的线程中填充的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:27