Code Bye

在android中,怎么样使用Handler更新ListView组件Adapter

由于刚学android,碰到一问题,本人想实现的功能是请求一个URL,返回一个JSON串, 然后将JSON解析成集合对象,将集合数据填充到ListView,取数据生成集合这个过程都没有问题,但是将数据填充到UI组件时有问题,下面是部分源码:
public class MainActivity extends AppCompatActivity {
    ListView listView;
    Context txContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txContext = this;
        listView = (ListView)findViewById(R.id.listView);
        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                //get NetWorkdata
                List<News> news = NewsDataUtil.getNewsForNetWork();
                System.out.println("======================getDataCount:" + news.size()+"");
                Message message = Message.obtain();
                message.obj = news;
                handler.handleMessage(message);
            }
        }).start();
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            List<News> news = (List<News>) msg.obj;
            //Update UI
            if (news!=null && news.size()>0)//set Adapter
                listView.setAdapter(new NewsListViewAdapter(txContext, news));
        }
    };
}

图片使用的是在线程中请求网络数据, 将数据存放在Message中,并通过handler对象handleMessage。
在handler的handleMessage中更新UI,执行后出现错误:

10-13 03:29:03.275 24742-24761/? E/AndroidRuntime: FATAL EXCEPTION: Thread-165
                                                   Process: com.example.rookie.listviewjson, PID: 24742
                                                   android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                       at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
                                                       at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:3034)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:761)
                                                       at android.view.View.setFlags(View.java:10555)
                                                       at android.view.View.setFocusableInTouchMode(View.java:7502)
                                                       at android.widget.AdapterView.checkFocus(AdapterView.java:737)
                                                       at android.widget.ListView.setAdapter(ListView.java:492)
                                                       at com.example.rookie.listviewjson.MainActivity$2.handleMessage(MainActivity.java:47)
                                                       at com.example.rookie.listviewjson.MainActivity$1.run(MainActivity.java:36)
                                                       at java.lang.Thread.run(Thread.java:818)
10-13 03:29:03.577 24742-24764/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa043730

不知道问题出在哪里,有知道的帮忙解释下,谢谢!

解决方案

40

在完成网络操作后,调用handler的sendMessage之类的方法将Message发送,这样才是正确的,然后在handler的handler的handleMessage方法中处理Message。这样发送Message的话,Handler内部的机制才会把Message的处理传到handler所在的线程,你写的也就是主线程,而你直接调用handleMessage方法,那么handleMessage就是在非主线程内部调用的,listview就在非主线程中设置adapter了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在android中,怎么样使用Handler更新ListView组件Adapter