Android: notifyDataSetChanged(); not working

后端 未结 8 1556
旧时难觅i
旧时难觅i 2020-11-28 10:33

I have a database in a server and from a Tablet I take some values from one table in the database. I load this information correctly into a list but I would like to know why

8条回答
  •  爱一瞬间的悲伤
    2020-11-28 11:06

    The update function should be called from UI thread. My answer is actually similar to @user1621629's answer with that difference that I am using rxJava, so here's working code that solve this problem for me:

    this.subscriber = myAdapter.getSubscriber(); // keep for unsubscribe in destroy
    dataSource.subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread()).subscribe(this.subscriber);
    

    So I set all execution in order to get data for the list to computation thread, but showing result in UI thread.

    Here's how I create subscriber for this:

    public class MyListAdapter extends RecyclerView.Adapter {
    
    
    private List mDataset = new ArrayList<>();
    
    public Subscriber getSubscriber() {
        return Subscribers.create(new Action1() {
            @Override
            public void call(ListItem[] listItems) {
                mDataset.clear();
                mDataset.addAll(Arrays.asList(listItems));
                notifyDataSetChanged();
            }
        });
    }
    ......
    

提交回复
热议问题