我正在尝试将我的代码从使用游标转换为使用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);
}
}
答案 0 :(得分:18)
要返回android.support.v4.app.LoaderManager
,您需要使用getSupportLoaderManager()。
不幸的是,ListActivity
无法使用此方法。
这可能会帮助你:
How to get LoaderManager in a ListActivity
答案 1 :(得分:1)
无法将LoaderManager解析为类型。 可以解决添加导入:
import android.support.v4.app.LoaderManager;
和Marcelo一样指出,使用getSupportLoaderManager()
代替getLoaderManager()