为了账号安全,请及时绑定邮箱和手机立即绑定

通过更改列表类型进行Android LiveData转换

通过更改列表类型进行Android LiveData转换

慕运维8079593 2021-05-01 11:27:28
我有这个视图模型public class MainViewModel extends AndroidViewModel {        // Constant for logging        private static final String TAG = MainViewModel.class.getSimpleName();        private LiveData<List<JournalEntry>> journals;        public MainViewModel(Application application) {            super(application);            AppDatabase database = AppDatabase.getInstance(this.getApplication());            Log.d(TAG, "Actively retrieving the tasks from the DataBase");            journals = database.journalDao().loadAllJournals();        }        public LiveData<List<JournalEntry>> getJournals() {            return journals;        }    }返回一个LiveData<List<JournalEntry>>我想转换实时数据以返回一个LiveData<List<ListItem>>ListItem列表包含JournalEntry对象和DateHeader对象的地方我曾试图像这样操纵观察清单private void setupViewModel() {        MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);        viewModel.getJournals().observe(this, new Observer<List<JournalEntry>>() {            @Override            public void onChanged(@Nullable List<JournalEntry> journalEntries) {                Log.d(TAG, "Updating list of tasks from LiveData in ViewModel");                Map<Date, List<JournalEntry>> journals = toMap(journalEntries);                Date previousDate = null;                for (Date date : journals.keySet()) {                    HeaderItem header = new HeaderItem(date);                    Date currentDate = header.getDate();                    if(previousDate==null || !DateUtil.formatDate(currentDate).equals(DateUtil.formatDate(previousDate))){                        items.add(header);                    }但是意识到视图模型正在复制onChange的所有项目,而不仅仅是更新已更改的项目。我不太确定如何使用LiveData转换实现此目标
查看完整描述

1 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

我有了MediatorLiveData想要的东西,然后使用每个onChanged清除了列表ItemList。我将MediatorLiveData值设置为ItemList的新列表,并进行观察


 private void setupViewModel() {

        MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        final LiveData<List<JournalEntry>> liveItems = viewModel.getJournals();

        final MediatorLiveData itemListData = new MediatorLiveData<>();

        itemListData.addSource(liveItems, new Observer<List<JournalEntry>>() {

            @Override public void onChanged(List<JournalEntry> journalEntries) {

                Map<Date, List<JournalEntry>> journals = toMap(journalEntries);

                Date previousDate = null;

                items.clear();

                for (Date date : journals.keySet()) {

                    HeaderItem header = new HeaderItem(date);

                    Date currentDate = header.getDate();

                    if (previousDate == null || !DateUtil.formatDate(currentDate).equals(DateUtil.formatDate(previousDate))) {

                        items.add(header);

                    }


                    for (JournalEntry journal : journals.get(date)) {

                        JournalItem item = new JournalItem(journal);

                        items.add(item);

                        previousDate = item.getJournalItem().getCreatedAt();

                    }

                }

                itemListData.setValue(items);



            }

        });


        itemListData.observe(this, new Observer<List<ListItem>>() {

            @Override

            public void onChanged(@Nullable List<ListItem> journalEntries) {

                Log.d(TAG, "Updating list of tasks from LiveData in ViewModel");

                mAdapter.setItems(journalEntries);

            }

        });



    }


查看完整回答
反对 回复 2021-05-12
  • 1 回答
  • 0 关注
  • 260 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号