Java 如何查询LiveData的房间数据库<;字符串>;在传递变量时
当我硬编码变量时,查询工作: 从道:Java 如何查询LiveData的房间数据库<;字符串>;在传递变量时,java,android,sql,android-room,Java,Android,Sql,Android Room,当我硬编码变量时,查询工作: 从道: @Query("SELECT amount FROM note_table WHERE item = 'Apple'") LiveData<String> getAmount(); @Query(“从note_表中选择金额,其中项目='Apple'”) LiveData getAmount(); 但当我试图传递一个变量时,它不会 @Query("SELECT amount FROM note_table
@Query("SELECT amount FROM note_table WHERE item = 'Apple'")
LiveData<String> getAmount();
@Query(“从note_表中选择金额,其中项目='Apple'”)
LiveData getAmount();
但当我试图传递一个变量时,它不会
@Query("SELECT amount FROM note_table WHERE item = :items")
LiveData<String> getAmountwithItem(String items);
@Query(“从note_表中选择金额,其中item=:items”)
LiveData getAmountwithItem(字符串项);
从ViewModel中:
private final LiveData<String> NoteAmount, NoteAmountwithItem;
NoteAmount = repository.getAmount();
NoteAmountwithItem = repository.getNoteAmountwithItem();
LiveData<String> getAmount() {
return NoteAmount;
}
LiveData<String> getNoteAmountwithItem() {
return NoteAmountwithItem;
}
LiveData<String> getNoteAmountwithItem(String item) {
return repository.getNoteAmountwithItem(item);
}
private final LiveData notemount,notamountwithitem;
NoteAmount=repository.getAmount();
NoteAmountwithItem=存储库。getNoteAmountwithItem();
LiveData getAmount(){
退货金额;
}
LiveData getNoteAmountwithItem(){
退货单金额与物品;
}
从存储库:
private final LiveData<String> NoteAmount, NoteAmountwithItem;
NoteAmount = noteDao.getAmount();
NoteAmountwithItem = noteDao.getAmountwithItem();
LiveData<String> getAmount() {
return NoteAmount;
}
LiveData<String> getNoteAmountwithItem() {
return NoteAmountwithItem;
}
LiveData<String> getNoteAmountwithItem(String item) {
return noteDao.getAmountwithItem(item);
}
private final LiveData notemount,notamountwithitem;
NoteAmount=noteDao.getAmount();
NoteAmountwithItem=noteDao.getAmountwithItem();
LiveData getAmount(){
退货金额;
}
LiveData getNoteAmountwithItem(){
退货单金额与物品;
}
来自MainActivity.java
noteViewModel.getAmount().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String queryAmount) {
if(queryAmount != null){
updateamount = String.valueOf(Integer.parseInt(queryAmount) + 1);
testTextView.setText("Amount: " + updateamount);
}else{
updateamount = "1";
}
}
});
noteViewModel.getAmount().observe(这个,新的观察者(){
@凌驾
公共void onChanged(@Nullable String queryAmount){
if(queryAmount!=null){
updateamount=String.valueOf(Integer.parseInt(queryAmount)+1);
setText(“金额:+updateamount”);
}否则{
updateamount=“1”;
}
}
});
这给了我QueryMount中苹果的数量,但我有不同的水果,希望将水果作为变量传递,并获得特定水果的数量。您需要将字符串参数从活动传递到
DAO
接口
在Dao中(保持与您的相同):
@Query(“从note_表中选择金额,其中item=:items”)
LiveData getAmountwithItem(字符串项);
在存储库中:
private final LiveData<String> NoteAmount, NoteAmountwithItem;
NoteAmount = noteDao.getAmount();
NoteAmountwithItem = noteDao.getAmountwithItem();
LiveData<String> getAmount() {
return NoteAmount;
}
LiveData<String> getNoteAmountwithItem() {
return NoteAmountwithItem;
}
LiveData<String> getNoteAmountwithItem(String item) {
return noteDao.getAmountwithItem(item);
}
LiveData getNoteAmountwithItem(字符串项){
返回说明dao.getAmountwithItem(item);
}
在ViewModel中:
private final LiveData<String> NoteAmount, NoteAmountwithItem;
NoteAmount = repository.getAmount();
NoteAmountwithItem = repository.getNoteAmountwithItem();
LiveData<String> getAmount() {
return NoteAmount;
}
LiveData<String> getNoteAmountwithItem() {
return NoteAmountwithItem;
}
LiveData<String> getNoteAmountwithItem(String item) {
return repository.getNoteAmountwithItem(item);
}
LiveData getNoteAmountwithItem(字符串项){
返回repository.getNoteAmountwithItem(item);
}
在活动中(传递要查找的字符串):
noteViewModel.getNoteAmountwithItem(“苹果”).observe(这是新的观察者(){
@凌驾
公共void onChanged(@Nullable String queryAmount){
if(queryAmount!=null){
updateamount=String.valueOf(Integer.parseInt(queryAmount)+1);
setText(“金额:+updateamount”);
}否则{
updateamount=“1”;
}
}
});
您是否向noteDao.getAmountwithItem(“苹果”)传递了字符串代码>?如果我将“苹果”添加到Reposity中的该语句中:NoteAmountwithItem=noteDao.getAmountwithItem(“苹果”);并使用getAmountwithItem,而不是从主活动获取金额。它可以工作,但如何从主活动传递字符串?请检查答案。。如果存储库中有不清楚的内容,请随时联系我们,并查看建模NoteAmountwithItem=noteDao.getAmountwithItem()代码>行需要一个参数。我放置了“”,但这是否覆盖了我在noteViewModel.getNoteAmountwithItem(items)中的main活动上设置的变量。请观察行?可以删除这些行。。我已经在答案中的方法中使用了它。这是我所问问题的正确答案。但是,LiveData并不是我试图解决的问题的正确解决方案@Zain帮助我实现了一个监听器接口,它在这种情况下非常有效。非常感谢。