android-是否有可能使两个LiveData中的一个LiveData?

前端之家收集整理的这篇文章主要介绍了android-是否有可能使两个LiveData中的一个LiveData? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有两个DAO,两个存储库和两个POJO.有什么方法可以创建两个Livedata?我需要它为Recyclerview制作一个清单.
POJO是相似的对象.

ExpenseRepository:

public class ExpenseRepository {

private ExpenseDao expenseDao;
private LiveData<List<Expense>> allExpenses;

public ExpenseRepository(Application application) {
    ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application);
    expenseDao = database.expenseDao();
    allExpenses = expenseDao.getExpensesByDay();
}

public LiveData<List<Expense>> getAllExpensesByDay() {
    return allExpenses;
}

收入库:

public class IncomeRepository {

private IncomeDao incomeDao;
private LiveData<List<Income>> allIncomes;

public IncomeRepository(Application application) {
    ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application);
    incomeDao = database.incomeDao();
    allIncomes = incomeDao.getIncomesByDay();
}

public LiveData<List<Income>> getAllIncomesByDay() {
    return allIncomes;
}

费用:

@Dao
public interface ExpenseDao {

@Query("SELECT * FROM expense_table ORDER BY day") 
LiveData<List<Expense>> getExpensesByDay();

IncomeDao:

@Dao
public interface IncomeDao {

@Query("SELECT * FROM income_table ORDER BY day") 
LiveData<List<Income>> getIncomesByDay();

Dailyviewmodel:

public class DailyFragmentviewmodel extends Androidviewmodel {

private ExpenseRepository expenseRepository;
private IncomeRepository incomeRepository;
private LiveData<Pair<List<Expense>,List<Income>>> combined;
private ExpenseDao expenseDao;
private IncomeDao incomeDao;

public DailyFragmentviewmodel(@NonNull Application application) {
    super(application);
    expenseRepository = new ExpenseRepository(application);
    incomeRepository = new IncomeRepository(application);
    combined = new DailyCombinedLiveData(expenseDao.getExpensesByDay(),incomeDao.getIncomesByDay());
}

public LiveData<Pair<List<Expense>,List<Income>>> getExpensesAndIncomes() {
    return combined;
}
最佳答案
我想您想将它们合并,是吗?您将需要一个MediatorLiveData,但是那个说您现在需要Object的人是错误的.您需要的是MediatorLiveData< Pair< List< Expense>,List< Income>>.

public class CombinedLiveData extends MediatorLiveData<Pair<List<Expense>,List<Income>>> {
    private List<Expense> expenses = Collections.emptyList();
    private List<Income> incomes = Collections.emptyList();

    public CombinedLiveData(LiveData<List<Expense>> ld1,LiveData<List<Income>> ld2) {
        setValue(Pai.create(expenses,incomes));

        addSource(ld1,(expenses) -> { 
             if(expenses != null) {
                this.expenses = expenses;
             } 
             setValue(Pair.create(expenses,incomes)); 
        });

        addSource(ld2,(incomes) -> { 
            if(incomes != null) {
                this.incomes = incomes;
            } 
            setValue(Pair.create(expenses,incomes));
        });
    }
}

您可能会使用这种通用名称,并且将使用2-arity(Pair)元组对两个LiveData进行CombineLatest的实现.

编辑:像这样:

public class CombinedLiveData2<A,B> extends MediatorLiveData<Pair<A,B>> {
    private A a;
    private B b;

    public CombinedLiveData2(LiveData<A> ld1,LiveData<B> ld2) {
        setValue(Pair.create(a,b));

        addSource(ld1,(a) -> { 
             if(a != null) {
                this.a = a;
             } 
             setValue(Pair.create(a,b)); 
        });

        addSource(ld2,(b) -> { 
            if(b != null) {
                this.b = b;
            } 
            setValue(Pair.create(a,b));
        });
    }
}

请注意,在这种情况下,我失去了将Collections.emptyList()设置为A和B的初始值的能力,并且在访问该对中的数据时,您将需要检查null.

猜你在找的Android相关文章