在单独的线程中填充列表视图 [英] Populating listview in separate thread

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

问题描述

在一个活动中,我加载了一个列表视图的行,这需要很多时间,因此我将此任务放在一个单独的线程中以允许显示进度对话框.

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.

bug 只是有时"出现,在逐步调试时不会出现.

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 填充列表视图并显示一个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 给出的关于Painless Threading"的文章,他们说

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

Android 提供了多种从其他线程访问 UI 线程的方法,其中之一是 HANDLER.

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

有人知道为什么将被解雇的人放在处理程序中没有解决我的问题吗?对我来说 listItem.add 与 UI 无关?在这一点上我错了吗?

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 ?

对我来说,在我的代码中唯一的 UI 是适配器和进度对话框?欢迎任何评论或解释.

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

最终答案

stjom 为我的特定代码提供了有效的答案.在处理程序中运行 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 ...

感谢大家的回答.

推荐答案

每当您调用 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();
    }  
});

在您的处理程序中.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆