android - LiveData onChanged 空结果
问题描述
事件道
@Dao
public interface EventDao {
@Query("SELECT * FROM events WHERE date_start = :date")
LiveData<List<EventPatientLocation>> test(Date date);
}
事件存储库
public class EventRepository {
private EventDao eventDao;
public EventRepository(Application application) {
MyDatabase db = MyDatabase.getDatabase(application);
eventDao = db.eventDao();
}
public LiveData<List<EventPatientLocation>> test(Date date) {
return eventDao.test(date);
}
事件视图模型
public class EventViewModel extends AndroidViewModel {
private EventRepository repository;
public EventViewModel(@NonNull Application application) {
super(application);
repository = new EventRepository(application);
}
public LiveData<List<EventPatientLocation>> test(Date date) {
return repository.test(date);
}
}
片段(onActivityCreated)
eventViewModel = ViewModelProviders.of(activity).get(EventViewModel.class);
LiveData<List<EventPatientLocation>> testEvent = eventViewModel.test(new Date());
testEvent.observe(activity, events -> {
// events is always null!
});
尽管片段上的 onChanged 回调被多次调用,但事件列表始终为空......我确定在数据库中有查询的候选行,那么我在哪里做错了?请帮忙...
解决方案
推荐阅读
- python - 有没有我可以用来将音频转换为音素的 python 库?
- python - 在测试环境中为 SQLAlchemy 模型创建表
- arrays - 在 Octave 中将长垂直 RGB 图像阵列重塑为分层的水平部分
- php - 使用连接的帖子的 MySQL 全选未选择(添加)标签
- oracle - 如何强制 Oracle 先解析 IN 子查询
- postgresql - 在普通 PostgreSQL 多边形数据中交换坐标
- python - 我们如何从数据框中的列中选择与特定模式匹配的项目?
- python - 踢球时,discord bot 无法发送直接消息(discord.py)
- javascript - 如何使用jquery在按钮单击时显示特定容器
- javascript - 如何使用 javascript 开发词法分析器?