我正在尝试将我的代码从使用游标转换为使用 CursorLoaders。为了支持旧版本的 Android,我安装了支持库,而不是导入 android.content.CursorLoader,而是导入 android.support.v4.content.CursorLoader。现在编译好的代码抛出了两个错误。当我实现它时,第一个错误出现在 LoaderManager 上。错误是:LoaderManager 无法解析为类型。第二个错误是调用 initLoader。错误是:LoaderManager 类型中的方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 不适用于参数(int, Bundle, MyList)。这是我的代码:
public class MyList extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String[] PROJECTION = new String[] { "_id", "fieldname" };
private static final int LOADER_ID = 0;
private static final String MYTABLE_BASEPATH = "MyTable_tbl";
private static final String AUTHORITY = "SQLData";
public static final Uri MY_URI = Uri.parse("content://" + AUTHORITY + "/" + MYTABLE_BASEPATH);
private SimpleCursorAdapter mAdapter;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent myData = getIntent();
Bundle info = myData.getExtras();
if (info != null){
Cursor c;
String[] dataColumns = { "fieldname" };
int[] viewIDs = { R.id.mylist1 };
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this, R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
getLoaderManager().initLoader(0, info, this);
}
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "level = '" + args.getString("Level") + "'";
return new CursorLoader(this, MY_URI,
PROJECTION, selection, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case LOADER_ID:
mAdapter.swapCursor(cursor);
break;
}
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}