使用AsyncTaskLoader动态载入SQLite数据
2014-03-07 12:21
211 查看
Andorid API Guides中有一章节ListView讲解了如何使用CursorLoader动态载入数据。但CursorLoader只支持Content Provider,实际上在很多小应用中都用不到Content Provider。我尝试继承CursorLoader的父类也就是AsyncTaskLoader实现了一个简单的动态载入SQLite数据的方案。如有不当之处希望指正。
NotesLoader继承AsyncTaskLoader
public class NotesLoader extends AsyncTaskLoader<Cursor> {
private NotesDbAdapter notesDbAdapter; //NotesDBAdapter可以在Android Example中找到,这里略作修改,以便支持多数据表的访问
public NotesLoader(Context context, String tableName) {
super(context);
notesDbAdapter = new NotesDbAdapter(context, tableName);
}
@Override
protected void onStartLoading() {
Log.v("onStartLoading", "OK");
forceLoad(); //强制加载
}
@Override
public Cursor loadInBackground() {
Log.v("loadInBackground","OK");
notesDbAdapter.open();
mCursor = notesDbAdapter.fetchAllNotes();
return mCursor;
}
@Override
protected void onStopLoading() {
Log.v("onStopLoading","OK");
notesDbAdapter.close();
}
}
PlaceholderFragment接口LoaderManager,换成Activity也是一样的
public class PlaceholderFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor>{
private Cursor mNotesCursor;
private SimpleCursorAdapter notesAdapter=null;
private int oldLoaderId;
private int newLoaderId;
public PlaceholderFragment() { }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.subject_date_text1};
notesAdapter =
new SimpleCursorAdapter(getActivity(), R.layout.row_subject_date, null, from, to, 0);
setListAdapter(notesAdapter);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
NotesUID note = new NotesUID();
Bundle extras = null;
if (data != null) {
extras = data.getExtras();
}
try {
if ({更换Cursor,对应在数据库中更换数据表}) {
getLoaderManager().destroyLoader(oldLoaderId);
getLoaderManager().initLoader(newLoaderId, null, this);
} else if ({刷新Cursor}) {
getLoaderManager().restartLoader(oldLoader,null,this);
}
} catch (Exception e) {
Log.e("PlaceholderFragment onActivityResult","error:"+e.getMessage());
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mNotesCursor != null)
mNotesCursor.close();
}
@Override
// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
Log.v("onCreateLoader","OK");
return new NotesLoader(getActivity(), {通过id找到数据表名});
}
@Override
// Called when a previously created loader has finished loading
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// 这里没有使用SwapCursor,而是使用了changeCursor,因为ChangeCursor比较明确老的cursor会被关闭,
//SwapCursor由于不太清楚机制,不太敢用
Log.v("onLoadFinished","OK");
notesAdapter.changeCursor(data);
mNotesCursor = data;
}
@Override
// Called when a previously created loader is reset, making the data unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
Log.v("onLoaderReset","OK");
notesAdapter.changeCursor(null);
}
}
NotesLoader继承AsyncTaskLoader
NotesLoader继承AsyncTaskLoader
public class NotesLoader extends AsyncTaskLoader<Cursor> {
private NotesDbAdapter notesDbAdapter; //NotesDBAdapter可以在Android Example中找到,这里略作修改,以便支持多数据表的访问
public NotesLoader(Context context, String tableName) {
super(context);
notesDbAdapter = new NotesDbAdapter(context, tableName);
}
@Override
protected void onStartLoading() {
Log.v("onStartLoading", "OK");
forceLoad(); //强制加载
}
@Override
public Cursor loadInBackground() {
Log.v("loadInBackground","OK");
notesDbAdapter.open();
mCursor = notesDbAdapter.fetchAllNotes();
return mCursor;
}
@Override
protected void onStopLoading() {
Log.v("onStopLoading","OK");
notesDbAdapter.close();
}
}
PlaceholderFragment接口LoaderManager,换成Activity也是一样的
public class PlaceholderFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor>{
private Cursor mNotesCursor;
private SimpleCursorAdapter notesAdapter=null;
private int oldLoaderId;
private int newLoaderId;
public PlaceholderFragment() { }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.subject_date_text1};
notesAdapter =
new SimpleCursorAdapter(getActivity(), R.layout.row_subject_date, null, from, to, 0);
setListAdapter(notesAdapter);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
NotesUID note = new NotesUID();
Bundle extras = null;
if (data != null) {
extras = data.getExtras();
}
try {
if ({更换Cursor,对应在数据库中更换数据表}) {
getLoaderManager().destroyLoader(oldLoaderId);
getLoaderManager().initLoader(newLoaderId, null, this);
} else if ({刷新Cursor}) {
getLoaderManager().restartLoader(oldLoader,null,this);
}
} catch (Exception e) {
Log.e("PlaceholderFragment onActivityResult","error:"+e.getMessage());
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (mNotesCursor != null)
mNotesCursor.close();
}
@Override
// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
Log.v("onCreateLoader","OK");
return new NotesLoader(getActivity(), {通过id找到数据表名});
}
@Override
// Called when a previously created loader has finished loading
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// 这里没有使用SwapCursor,而是使用了changeCursor,因为ChangeCursor比较明确老的cursor会被关闭,
//SwapCursor由于不太清楚机制,不太敢用
Log.v("onLoadFinished","OK");
notesAdapter.changeCursor(data);
mNotesCursor = data;
}
@Override
// Called when a previously created loader is reset, making the data unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
Log.v("onLoaderReset","OK");
notesAdapter.changeCursor(null);
}
}
NotesLoader继承AsyncTaskLoader
相关文章推荐
- extjs的tree的使用(拖动、动态载入json数据、拖动后的事件处理)
- extjs的tree的使用(拖动、动态载入json数据、拖动后的事件处理)
- listview动态添加数据,使用sqlite对本地数据库增删改查
- 第一次使用echart从后台获取数据动态显示到页面
- Android 用SQLite 使用 CursorLoader 中的数据填充列表视图
- hive导入CSV数据,使用动态分区重新分区
- Echarts的使用以及动态加载数据
- 使用AngularJS结合Highchart图表动态获取JSON格式数据
- silverlight3 datagrid c#中动态生成模板列(日期格式)使用IValueConvert对绑定数据的格式化操作
- listview滚到底时动态载入更多系统联系人列表。SQL Limit的使用
- iOS开发24:使用SQLite3存储和读取数据
- Qt:使用Model-View,动态的加载显示数据
- 使用 JSP 技术和 JDBC 技术访问基于 Web 的动态数据
- 动态使用结构数据大小
- ListView动态加载数据分页(使用Handler+线程和AsyncTask两种方法)
- HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>
- Highcharts使用easyui datagrid的数据画动态统计图
- js动态添加表格数据使用insertRow和insertCell实现
- 动态载入数据的无刷新TreeView控件(2)