如何使用实时数据编辑视图模型/存储库中的条目



也许我误解了这一切,但如何使用实时数据/视图模型/存储库编辑数据?我看到了如何查询所有数据,删除一个条目,但我想编辑一个特定的字段。

例如,我有3件事正在跟踪。日期、时间和类别。我需要能够更改类别。

我试着在我的一项活动中做这样的事情:

budgetViewModel.getAllEntries().observe(this, new Observer<List<BudgetEntry>>() {
@Override
public void onChanged(List<BudgetEntry> budgetEntries) {

Log.i(TAG, "2nd Observer is going off");
if (manualUpdate == 1) {
Log.i(TAG, "MANUAL UPDATE");
budgetEntries.get(0).setCategory(updatedCategory);
manualUpdate = 0;
Log.i(TAG, "Budget Entries: " + budgetEntries.get(0).getCategory());
}
else {
Log.i(TAG, "No change");
}

}
});
}
}

但它不会永久改变它。我想只有在这个例子中,因为日志显示我更改了类别,但当我重新加载应用程序或检查另一个活动时,它仍然显示以前的数据。

我找不到任何关于这方面的教程,所以任何指导都将不胜感激!

新视图模型更改。我得到错误无法创建类的实例:

公共类BudgetViewModel扩展了AndroidViewModel{

private BudgetRepository budgetRepository;
//private LiveData<List<BudgetEntry>> allEntries;
LiveData<List<BudgetEntry>> _allEntries = budgetRepository.getAllEntries();
LiveData<List<BudgetEntry>> allEntries = Transformations.map(_allEntries, new Function<List<BudgetEntry>, List<BudgetEntry>>() {
@Override
public List<BudgetEntry> apply(List<BudgetEntry> input) {
return input;
}
});

public BudgetViewModel(@NonNull Application application) {
super(application);
budgetRepository = new BudgetRepository(application);
//allEntries = budgetRepository.getAllEntries();

}
public void insert (BudgetEntry budgetEntry) {
budgetRepository.insert(budgetEntry);
}

public void delete (BudgetEntry budgetEntry) {
budgetRepository.delete(budgetEntry);
}
public void deleteAllEntries () {
budgetRepository.deleteAllEntries();
}
public LiveData<List<BudgetEntry>> getAllEntries() {
return _allEntries;
}
public LiveData<Integer> getCount() {
return budgetRepository.getCount();
}

}

谢谢!

您可以在ViewModel中使用Transformations.map函数。

将主线程上的给定函数应用于源LiveData发出的每个值,并返回LiveData,后者会发出结果值

给定的函数func将在主线程上执行。

Kotlin

import androidx.lifecycle.*
class YourViewModel : ViewModel() {
private val _allEntries: LiveData<List<BudgetEntry>> = budgetRepository.getAllEntries()
val allEntries: LiveData<List<BudgetEntry>> = Transformations.map(_allEntries) { list: List<BudgetEntry> ->
list.map { budgetEntry ->
budgetEntry.setCategory(updatedCategorya)
}
}
}

Java

import androidx.lifecycle.*;
class YourViewModel extends ViewModel {
private final LiveData<List<BudgetEntry>> _allEntries = budgetRepository.getAllEntries();
final LiveData<List<BudgetEntry>> allEntries = Transformations.map(_allEntries, new Function<List<BudgetEntry>, List<BudgetEntry>>() {
@Override
public List<BudgetEntry> apply(List<BudgetEntry> input) {
// do the mapping for each budgetEntry
return // mapped list
}
});
}

现在您可以像往常一样观察allEntriesmap功能负责更新LiveData。

我终于想通了,哇!我无法得到转换的答案,所以我开始研究如何使用@Update。我想分享一下,以防这对将来的人有所帮助,因为这一次花了我几个星期的时间。:(

我在我的模型中跟踪了3件事。类别、日期、时间。我的目标是能够将所有与特定字符串匹配的类别名称更改为新字符串。

首先,你需要在你的模型中命名你的列:

@ColumnInfo(名称="CATEGORY"(private字符串类别;

然后,您需要在DAO中创建一个SQL查询,该查询查找旧类别并用新类别更新:

@Query("UPDATE entry SET CATEGORY = :newcategory WHERE category = :oldcategory")
void updateColumn(String newcategory, String oldcategory);

这基本上是说找到所有与字符串匹配的类别,并将它们更新为新字符串。

现在,您需要在视图模型和存储库中制作方法。

我们将从存储库开始制作异步任务。首先,我需要将旧类别和新类别捆绑在一起,这样我就可以在存储库中创建一个类。

private static class ParamsSend {
String newCategory;
String oldCategory;
ParamsSend(String newCategory, String oldCategory) {
this.newCategory = newCategory;
this.oldCategory = oldCategory;
}
}

然后我将创建一个传递这些参数的异步任务。

私有静态类UpdateColumnAsyncTask扩展了AsyncTask<参数发送,无效,无效>{私人预算Dao-BudgetDao;

private UpdateColumnAsyncTask(BudgetDao budgetDao) {
this.budgetDao = budgetDao;
}
@Override
protected Void doInBackground(ParamsSend... paramsSends) {
String newCategory = paramsSends[0].newCategory;
String oldCategory = paramsSends[0].oldCategory;
budgetDao.updateColumn(newCategory, oldCategory);
return null;
}
}

然后创建方法来调用异步任务:

public void updateColumn(String newCategory, String oldCategory) {
ParamsSend paramsSend  = new ParamsSend(newCategory, oldCategory);
new UpdateColumnAsyncTask(budgetDao).execute(paramsSend);
}

然后,我们将转到ViewModel并创建一个方法来运行Repository中的异步任务:

public void updateColumn(String newCategory, String oldCategory) {
budgetRepository.updateColumn(newCategory, oldCategory);

最后,我们将在我们的活动中更新它:

budgetViewModel.updateColumn(updatedCategory, oldCategory);

祝大家好运,我希望这能有所帮助!:-(

最新更新