Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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;
}
最佳答案
我想您想将它们合并,是吗?您将需要@L_206_2@mediatorLiveData,但是那个说您现在需要Object的人是错误的.您需要的是MediatorLiveData<><>< 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-是否有可能使两个LiveData中的一个LiveData? 全部内容,希望文章能够帮你解决android-是否有可能使两个LiveData中的一个LiveData? 所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。