7

我正在尝试将我的代码从使用游标转换为使用 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);

}

}

4

2 回答 2

18

为了返回android.support.v4.app.LoaderManager,您需要使用getSupportLoaderManager()。不幸的是,此方法不适用于ListActivity. 这可能会对您有所帮助: How to get LoaderManager in a ListActivity

于 2012-10-23T21:49:10.647 回答
1

LoaderManager 无法解析为 type。可以解决添加导入:

import android.support.v4.app.LoaderManager;

就像马塞洛指出的那样,使用getSupportLoaderManager()而不是getLoaderManager()

于 2014-11-26T17:06:59.470 回答