ListActivity中的getLoaderManager

时间:2011-10-01 20:55:36

标签: android listactivity android-loadermanager

我希望在ListActivity中实现Loader,但活动无法识别getLoaderManager。

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);


    dbHelper =  new DBHelper(this,DBNAME,FindPackageName(), TABLE_NAME);

    sql = dbHelper.getReadableDataBase();
    //Log.d("Gaurav","Database Open");
    String[] from = new String[]{"word","_id","MyList"};
    int[] to = new int[]{R.id.listrow };

    simpleCursorLoader = new SimpleCursorLoader(this, TABLE_NAME, from, null, null, null, null, null, null, sql);



    //query result will be whole database
    //cursor = sql.query(TABLE_NAME, from, null, null, null, null, null);
    //startManagingCursor(cursor); //this method is deprecated
    //Log.d(TAG,"Cursor Set");



    completerOrMyListAdapter = new CompleteOrMyListAdapter(this,
            R.layout.completeormylist_adapter, cursor, from, to, dbHelper); 
    setListAdapter(completerOrMyListAdapter);  

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    LoaderManager lm = getLoaderManager();
    //if (lm.getLoader(0) != null) {
    //    lm.initLoader(0, null, this);
    //}
    //getLoaderManager().initLoader(0, null, this);
}

2 个答案:

答案 0 :(得分:19)

如果您的应用仅在API级别11或更高级别上运行,请相应地设置您的构建目标,并且该方法将可用。

但是,如果您使用Android兼容性库在API级别11之前支持加载程序,则无法使用ListActivity。你必须继承FragmentActivity。要么使用ListFragment,要么只使用自己管理的普通ListView

答案 1 :(得分:11)

我认为您可能会使用以下代替

getSupportLoaderManager().initLoader(0, null, this); 

如果您使用支持v4包