您的位置:首页 > 数据库

使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息