android-房间查询返回null
作者:互联网
我正在尝试确定数据库中是否存在具有特定ID的项目,但是即使我知道它在数据库中,我仍会得到null.我很确定自己定义的查询不正确,但不确定是什么问题.我一直在查看“房间”查询示例,但无法解决该问题.我在运行代码时没有看到任何错误,除了在尝试打印查询日志时出现空指针异常.
我正在DAO中尝试执行的查询如下所示:
@Query("select filmID from FilmDataModel where filmID = :currentId")
LiveData<String> inDatabase(String currentId);
我的视图模型使用这种方法充当UI和DAO之间的过渡:
public LiveData<String> inDatabase(String currentID) {
LiveData<String> filmID = filmDatabase.filmDao().inDatabase(currentID);
return filmID;
}
我的FilmDataModel对象定义如下:
@Entity public class FilmDataModel {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "posterURL")
private String posterURL;
private String releaseDate;
@ColumnInfo(name = "voterAvg")
private String voterAvg;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "filmID")
private String filmId;
public FilmDataModel(String posterURL, String releaseDate, String voterAvg, String description,
String title, String filmId) {
this.posterURL = posterURL;
this.releaseDate = releaseDate;
this.voterAvg = voterAvg;
this.description = description;
this.title = title;
this.filmId = filmId;
}
public String getPosterURL() {
return this.posterURL;
}
public String getReleaseDate() {
return releaseDate;
}
public String getDescription() {
return description;
}
public String getFilmId() {
return filmId;
}
public String getTitle() {
return title;
}
public String getVoterAvg() {
return voterAvg;
}
}
然后在我的活动中,将以下代码设置为单击按钮:
favoriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Favorite button pressed.");
FilmDataModel filmDataModel = new FilmDataModel(posterURL, releaseDate, voterAvg,
overview, title, id);
if (favoriteSelected) {
Log.d(TAG, "Delete Item.");
favoriteSelected = false;
favoriteButton.setImageResource(R.drawable.heart_icon_off);
viewModel.deleteItem(filmDataModel);
} else {
Log.d(TAG, "Insert Item.");
favoriteSelected = true;
favoriteButton.setImageResource(R.drawable.heart_icon_on);
viewModel.insertItem(filmDataModel);
Log.d(TAG, viewModel.inDatabase(id).getValue());
}
}
});
看起来数据是基于活动和视图模型的打印语句插入数据库的,并且可以从数据库中删除,但是当我尝试运行查询并打印filmID时,我得到一个空值.我可以不执行该活动要执行的操作吗?我的查询语句由于某种原因不正确吗?
解决方法:
从Android Developers documentation开始:
LiveData is a data holder class that can be observed within a given
lifecycle
这意味着您必须先注册一个Observer才能接收数据更新.
同样,从Android Developers documentation开始:
Generally, LiveData delivers updates only when data changes, and only
to active observers. An exception to this behavior is that observers
also receive an update when they change from an inactive to an active
state. Furthermore, if the observer changes from inactive to active a
second time, it only receives an update if the value has changed since
the last time it became active.
这是一个可能的解决方案:
favoriteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Favorite button pressed.");
FilmDataModel filmDataModel = new FilmDataModel(posterURL, releaseDate, voterAvg, overview, title, id);
if (favoriteSelected) {
Log.d(TAG, "Delete Item.");
} else {
Log.d(TAG, "Insert Item.");
favoriteSelected = true;
favoriteButton.setImageResource(R.drawable.heart_icon_on);
viewModel.insertItem(filmDataModel);
viewModel.inDatabase(id).observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable final String name) {
// your code here
Log.d(TAG, name.getValue());
}
});
}
}
});
有关更多信息,请参见Work with LiveData objects
标签:android-room,android-architecture-components,android 来源: https://codeday.me/bug/20191108/2010444.html