如何使用实时数据编辑视图模型/存储库中的条目
How to edit an entry inside View Model / Repository with Live Data
也许我误解了这一切,但是如何使用 Live Data/View Model/Repository 编辑数据?我看到如何查询所有数据,删除条目,但我想编辑特定字段。
例如,我正在跟踪 3 件事。日期、时间和 Category.I 需要能够更改类别。
我尝试在我的一项活动中做这样的事情:
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");
}
}
});
}
}
但它不会永久改变它。我猜只是对于这个实例,因为日志显示我更改了类别,但是当我重新加载应用程序或检查另一个应用程序时 activity,它仍然显示先前的数据。
我找不到这方面的任何教程,因此非常感谢任何指导!
新的视图模型更改。我得到错误无法创建 class:
的实例
public class 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 函数。
Applies the given function on the main thread to each value emitted by source LiveData and returns LiveData, which emits resulting values.
The given function func will be executed on the main thread.
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
}
});
}
现在您可以照常观察allEntries
了。 map
函数负责更新 LiveData。
好吧,我终于想通了,哇哦!我无法得到 Transformation 的答案,所以我开始研究如何使用 @Update。我想分享一下,以防将来对某人有所帮助,因为这花了我几个星期的时间。 :)
我在我的模型中跟踪了 3 件事。类别、日期、时间。我的目标是能够将与特定字符串匹配的所有类别名称更改为新字符串。
首先您需要在您的模型中命名您的列:
@ColumnInfo(name="类别")
私有字符串类别;
然后您需要在您的 DAO 中创建一个 SQL 查询,以查找旧类别并将其更新为新类别:
@Query("UPDATE entry SET CATEGORY = :newcategory WHERE category = :oldcategory")
void updateColumn(String newcategory, String oldcategory);
这基本上是说找到与字符串匹配的所有类别,并将它们更新为您的新字符串。
现在您需要在视图模型和存储库中创建方法。
我们将从存储库开始创建一个异步任务。首先,我需要将旧类别和新类别捆绑在一起,以便在存储库中创建一个 class。
private static class ParamsSend {
String newCategory;
String oldCategory;
ParamsSend(String newCategory, String oldCategory) {
this.newCategory = newCategory;
this.oldCategory = oldCategory;
}
}
然后我将创建一个传递这些参数的异步任务。
private static class UpdateColumnAsyncTask extends AsyncTask {
私人 BudgetDao 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 并为 运行 Repoository 中的异步任务创建一个方法:
public void updateColumn(String newCategory, String oldCategory) {
budgetRepository.updateColumn(newCategory, oldCategory);
最后我们会在 activity:
中更新它
budgetViewModel.updateColumn(updatedCategory, oldCategory);
祝大家好运,希望对您有所帮助! :-)
也许我误解了这一切,但是如何使用 Live Data/View Model/Repository 编辑数据?我看到如何查询所有数据,删除条目,但我想编辑特定字段。
例如,我正在跟踪 3 件事。日期、时间和 Category.I 需要能够更改类别。
我尝试在我的一项活动中做这样的事情:
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");
}
}
});
}
}
但它不会永久改变它。我猜只是对于这个实例,因为日志显示我更改了类别,但是当我重新加载应用程序或检查另一个应用程序时 activity,它仍然显示先前的数据。
我找不到这方面的任何教程,因此非常感谢任何指导!
新的视图模型更改。我得到错误无法创建 class:
的实例public class 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 函数。
Applies the given function on the main thread to each value emitted by source LiveData and returns LiveData, which emits resulting values.
The given function func will be executed on the main thread.
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
}
});
}
现在您可以照常观察allEntries
了。 map
函数负责更新 LiveData。
好吧,我终于想通了,哇哦!我无法得到 Transformation 的答案,所以我开始研究如何使用 @Update。我想分享一下,以防将来对某人有所帮助,因为这花了我几个星期的时间。 :)
我在我的模型中跟踪了 3 件事。类别、日期、时间。我的目标是能够将与特定字符串匹配的所有类别名称更改为新字符串。
首先您需要在您的模型中命名您的列:
@ColumnInfo(name="类别") 私有字符串类别;
然后您需要在您的 DAO 中创建一个 SQL 查询,以查找旧类别并将其更新为新类别:
@Query("UPDATE entry SET CATEGORY = :newcategory WHERE category = :oldcategory")
void updateColumn(String newcategory, String oldcategory);
这基本上是说找到与字符串匹配的所有类别,并将它们更新为您的新字符串。
现在您需要在视图模型和存储库中创建方法。
我们将从存储库开始创建一个异步任务。首先,我需要将旧类别和新类别捆绑在一起,以便在存储库中创建一个 class。
private static class ParamsSend {
String newCategory;
String oldCategory;
ParamsSend(String newCategory, String oldCategory) {
this.newCategory = newCategory;
this.oldCategory = oldCategory;
}
}
然后我将创建一个传递这些参数的异步任务。
private static class UpdateColumnAsyncTask extends AsyncTask
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 并为 运行 Repoository 中的异步任务创建一个方法:
public void updateColumn(String newCategory, String oldCategory) {
budgetRepository.updateColumn(newCategory, oldCategory);
最后我们会在 activity:
中更新它 budgetViewModel.updateColumn(updatedCategory, oldCategory);
祝大家好运,希望对您有所帮助! :-)