getLoaderManager in ListActivity

Gaurav Agarwal picture Gaurav Agarwal · Oct 1, 2011 · Viewed 8.1k times · Source

I wish to implement a Loader for in a ListActivity but the activity do not recognize 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);
}

Answer

CommonsWare picture CommonsWare · Oct 1, 2011

If your app will only run on API Level 11 or higher, set your build target appropriately, and the method will be available.

However, if you are using the Android Compatibility Library to have support for loaders before API Level 11, you cannot use ListActivity. You have to inherit from FragmentActivity. Either use a ListFragment, or just a plain ListView that you manage yourself.