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

时间:2018-07-04 08:43:06

标签: java android android-livedata

我有这个视图模型

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);
                    }

                    for (JournalEntry journal : journals.get(date)) {
                        JournalItem item = new JournalItem(journal);
                        items.add(item);
                        previousDate = item.getJournalItem().getCreatedAt();
                    }
                }
                mAdapter.setItems(items);
            }
        });
    }

但是意识到视图模型正在复制onChange的所有项目,而不仅仅是更新已更改的项目。我不太确定如何使用LiveData转换实现此目标

预先感谢

1 个答案:

答案 0 :(得分:0)

我用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);
            }
        });


    }