Java r真正详细的解释!这对我帮助很大!我尝试使用Dao作为您给出的最后一个示例,但由于我不太清楚如何生成多个线程,因此我最终求助于LiveData,因为默认情况下,它在后台执行查询。你的解释让我很容易理解,为此我真的很感激! ViewModelEntry re
r真正详细的解释!这对我帮助很大!我尝试使用Dao作为您给出的最后一个示例,但由于我不太清楚如何生成多个线程,因此我最终求助于LiveData,因为默认情况下,它在后台执行查询。你的解释让我很容易理解,为此我真的很感激!Java r真正详细的解释!这对我帮助很大!我尝试使用Dao作为您给出的最后一个示例,但由于我不太清楚如何生成多个线程,因此我最终求助于LiveData,因为默认情况下,它在后台执行查询。你的解释让我很容易理解,为此我真的很感激! ViewModelEntry re,java,android,android-room,Java,Android,Android Room,r真正详细的解释!这对我帮助很大!我尝试使用Dao作为您给出的最后一个示例,但由于我不太清楚如何生成多个线程,因此我最终求助于LiveData,因为默认情况下,它在后台执行查询。你的解释让我很容易理解,为此我真的很感激! ViewModelEntry recentEntry = new ViewModelEntry(RegisterActivity.this.getApplication()); int lastCadID = recentEntry.getLastEntry().getValu
ViewModelEntry recentEntry = new ViewModelEntry(RegisterActivity.this.getApplication());
int lastCadID = recentEntry.getLastEntry().getValue().getCadID();
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Integer com.xxxx.xxxx.entities.SchoolEntry.getCadID()' on a null object
repository = new ReportRepository(application);
public LiveData<SchoolEntry> getLastEntry() {return repository.getLastEntry(); }
ReportDatabase db = ReportDatabase.getDatabase(application);
schoolEntryDao = db.schoolEntryDao();
public LiveData<SchoolEntry> getLastEntry() { return schoolEntryDao.getLastEntry(); }
@Query("SELECT * FROM SchoolEntry WHERE cadID = (SELECT MAX(cadID) from SchoolEntry)")
LiveData<SchoolEntry> getLastEntry();
recentEntry.getLastEntry().observe(this, lastEntry ->
{
// do something with lastEntry.getCatId();
});
recentEntry.getLastEntry().observe(this, new Observer<SchoolEntry>() {
@Override
public void onChanged(SchoolEntry lastEntry) {
//do something with lastEntry.getCatId()
}
});
LiveData<SchoolEntry> lastEntryLiveData = recentEntry.getLastEntry();
// "this" is used here assuming this class is an Activity, as I inferred from your code above
lastEntryLiveData.observe(this, new Observer<SchoolEntry>() {
@Override
public void onChanged(SchoolEntry result) {
int lastCadID = result.getCadID();
// you can use lastCadID here, and this method may be called
// multiple times whenever your database changes and the query
// "SELECT * FROM SchoolEntry WHERE cadID = (SELECT MAX(cadID) from SchoolEntry)"
// would return a new result
}
});
@Query("SELECT * FROM SchoolEntry WHERE cadID = (SELECT MAX(cadID) from SchoolEntry)")
SchoolEntry getLastEntry();