How to use notifyDataSetChanged() in thread

前端 未结 5 1871
醉话见心
醉话见心 2020-12-03 05:00

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

private class ReceiverThread extends Thread {

@Override
         


        
5条回答
  •  一个人的身影
    2020-12-03 05:35

    access the UI thread from other threads

    Activity.runOnUiThread(Runnable)

    View.post(Runnable)

    View.postDelayed(Runnable, long)

    my approach whe i use other Threads for work:

    private AbsListView _boundedView;
    private BasicAdapter _syncAdapter;
    
     /** bind view to adapter */
    public void bindViewToSearchAdapter(AbsListView view) {
        _boundedView = view;
        _boundedView.setAdapter(_syncAdapter);
    }
    
    /** update view on UI Thread */
    public void updateBoundedView() {
        if(_boundedView!=null) {
            _boundedView.post(new Runnable() {
                @Override
                public void run() {
                    if (_syncAdapter != null) {
                        _syncAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }
    

    btw notifyDatasetChanged() method hooks to DataSetObservable class object of AbsListView which is set first by involving AbsListView.setAdaptert(Adapter) method by setting callback to Adapter.registerDataSetObserver(DataSetObserver);

提交回复
热议问题