gpt4 book ai didi

android - 是否可以将两个 LiveData 做成一个 LiveData?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:56 28 4
gpt4 key购买 nike

我有两个 DAO、两个存储库和两个 POJO。有什么方法可以创建两个的一个 Livedata?我需要它来为 Recyclerview 制作单个列表。POJO 是类似的对象。

费用库:

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

收入道:

@Dao
public interface IncomeDao {

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

每日 View 模型:

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(Pair.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));
});
}
}

您可能会使其成为通用的,它会成为 combineLatest 的实现使用 2-arity (Pair) 元组的两个 LiveData。

编辑:像这样:

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 s 当您访问对中的数据时。

编辑:您可以使用库 https://github.com/Zhuinden/livedata-combinetuple-kt ( Kotlin )或 https://github.com/Zhuinden/livedata-combineutil-java/ (Java) 做同样的事情。

关于android - 是否可以将两个 LiveData 做成一个 LiveData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044298/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com