如何在线程中使用notifyDataSetChanged() [英] How to use notifyDataSetChanged() in thread

查看:908
本文介绍了如何在线程中使用notifyDataSetChanged()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个线程来更新我的数据,并尝试做 notifyDataSetChanged 在我的ListView。

I create a thread to update my data and try to do notifyDataSetChanged at my ListView.

private class ReceiverThread extends Thread {

@Override
public void run() { 
    //up-to-date
    mAdapter.notifyDataSetChanged();
}

出现在行错误:

The error occurs at line:

mAdapter.notifyDataSetChanged();

错误

12-29 16:44:39.946:E / AndroidRuntime(9026):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程

12-29 16:44:39.946: E/AndroidRuntime(9026): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我应该如何修改呢?

推荐答案

使用 runOnUiThread()方法来执行从非UI线程的UI操作。

Use runOnUiThread() method to execute the UI action from a Non-UI thread.

private class ReceiverThread extends Thread {
@Override
public void run() { 
Activity_name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
             mAdapter.notifyDataSetChanged();
        }
    });
}

这篇关于如何在线程中使用notifyDataSetChanged()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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