1. 程式人生 > >在android中,如何使用Handler更新ListView元件Adapter?

在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


不知道問題出在哪裡,有知道的幫忙解釋下,謝謝!