房间查询 returns 无
Room query returns 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;
}
}
然后从我的 Activity 我在单击按钮时设置以下代码:
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());
}
}
});
根据 activity 和视图模型的打印语句,看起来数据已插入数据库并从数据库中删除,但是当我尝试 运行 查询并打印filmID 我得到一个空值。我可以不做我想从 Activity 做的事吗?我的查询语句是否由于某种原因不正确?
来自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
我正在尝试确定我的数据库中是否存在具有特定 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;
}
}
然后从我的 Activity 我在单击按钮时设置以下代码:
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());
}
}
});
根据 activity 和视图模型的打印语句,看起来数据已插入数据库并从数据库中删除,但是当我尝试 运行 查询并打印filmID 我得到一个空值。我可以不做我想从 Activity 做的事吗?我的查询语句是否由于某种原因不正确?
来自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