我正在使用room
库android
.当我在数据库中插入内容时,我获取当前正在运行的应用程序的数据,但是当我重新启动应用程序时,屏幕上没有任何内容.如何获取以前存储的数据without using LiveData
?
如果您期待获得以前的数据,以下Codelab可能会对您有所帮助.但这实际上取决于您的实施.
如何在不使用LiveData的情况下获取以前存储的数据?如何
room
使用库中存储的所有行Async Task
?
说明:
在你的Dao中包含以下代码:
@Query("SELECT * FROM user")
List<CustomObject> getAll();
Run Code Online (Sandbox Code Playgroud)
在您的Repository类中包含以下代码段:
public List<CustomObject> getAll() throws ExecutionException, InterruptedException {
return new getAllAsyncTask(mCustomObjectDao).execute().get();
}
private static class getAllAsyncTask extends android.os.AsyncTask<Void, Void, List<CustomObject>> {
private CustomObjectDao mAsyncTaskDao;
List<CustomObject> a;
getAllAsyncTask(CustomObjectDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected List<CustomObject> doInBackground(Void... voids) {
return mAsyncTaskDao.getAll();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1403 次 |
最近记录: |