首页 > 解决方案 > 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 回调被多次调用,但事件列表始终为空......我确定在数据库中有查询的候选行,那么我在哪里做错了?请帮忙...

标签: androidobservablerx-javadaoandroid-room

解决方案


推荐阅读