使用 Loader 在 Activity 个生命周期中查询一次数据库
Query DB once in Activity life-cycle using Loader
我正在完成一个项目,该项目使用 Singleton 作为模式,使用 SQLite 作为数据库。
我的想法是,我不想在 activity 生命周期中每次触发 onCreate 方法时都进行 select 查询,相反,我想要的是当配置更改或当 activity y 重新创建时,适配器使用之前加载的相同数据。
我怎么不使用 Content Provider 我不能使用 Loader 或 CursorLoader 所以我不知道该怎么做。
我的 MainActivity 代码如下:
RecyclerView recyclerView;
public Cursor cursor;
InsectRecyclerAdapter insectAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
cursor = DatabaseManager.getInstance(this).queryAllInsects("friendlyName"); //EVERY TIME THIS METHOD IS TRIGGER EXECUTE THE QUERY..AND I DON'T WANT THAT.
insectAdapter = new InsectRecyclerAdapter(this, cursor);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setAdapter(insectAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
我总是将 SQLite 与提供程序一起使用,所以这种方法对我来说是新的。
有什么建议吗?
我是用 AsyncTaskLoader 做的。这是代码:
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
private Cursor mCursor;
public SimpleCursorLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
//If the cursor is null call loadInBackground else deliverResult
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
@Override
public Cursor loadInBackground(){
mCursor = DatabaseManager.getInstance(MainActivity.this).queryAllInsects(MainActivity.FILTER);
return mCursor;
}
/* Runs on the UI thread */
@Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
insectAdapter.swapCursor(cursor);
}
return;
}
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
}
}
我正在完成一个项目,该项目使用 Singleton 作为模式,使用 SQLite 作为数据库。
我的想法是,我不想在 activity 生命周期中每次触发 onCreate 方法时都进行 select 查询,相反,我想要的是当配置更改或当 activity y 重新创建时,适配器使用之前加载的相同数据。
我怎么不使用 Content Provider 我不能使用 Loader 或 CursorLoader 所以我不知道该怎么做。
我的 MainActivity 代码如下:
RecyclerView recyclerView;
public Cursor cursor;
InsectRecyclerAdapter insectAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
cursor = DatabaseManager.getInstance(this).queryAllInsects("friendlyName"); //EVERY TIME THIS METHOD IS TRIGGER EXECUTE THE QUERY..AND I DON'T WANT THAT.
insectAdapter = new InsectRecyclerAdapter(this, cursor);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setAdapter(insectAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
我总是将 SQLite 与提供程序一起使用,所以这种方法对我来说是新的。
有什么建议吗?
我是用 AsyncTaskLoader 做的。这是代码:
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
private Cursor mCursor;
public SimpleCursorLoader(Context context) {
super(context);
}
@Override
protected void onStartLoading() {
//If the cursor is null call loadInBackground else deliverResult
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
@Override
public Cursor loadInBackground(){
mCursor = DatabaseManager.getInstance(MainActivity.this).queryAllInsects(MainActivity.FILTER);
return mCursor;
}
/* Runs on the UI thread */
@Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
insectAdapter.swapCursor(cursor);
}
return;
}
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
}
}