getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks

后端 未结 19 1575
无人共我
无人共我 2020-12-07 16:19

I\'m having trouble following a guide on using SQLite in Android. I\'m using a ListFragment instead of a ListActivity(as in the example), so I have

相关标签:
19条回答
  • 2020-12-07 17:06

    Try these 2 lines it will work

    android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();
    
    loaderManager.initLoader(LOADER_ID, null,  this);
    
    0 讨论(0)
  • 2020-12-07 17:08

    You are not using the right implementations of CursorLoader and Loader. Remove your old imports and use these ones:

    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
    import android.support.v4.widget.CursorAdapter;
    

    But I have the same Problem using SherlockActionBar: As I have to extend SherlockListActivity there is NO method getSupportLoadManager().

    Any ideas on this?

    EDIT: follow this tutorial if you do not know how to use fragments. Create a new Class with extends SherlockFragment and move your display logic there. Make your old activity extend SherlockFragmentActivity and show the newly created SherlockFragment. This way I got it working. Thanks to @JakeWharton!

    0 讨论(0)
  • 2020-12-07 17:10

    I was trying to call initLoader() inside an activity. For me this worked

    changing

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

    to

    getLoaderManager().initLoader(LOADER_ID,null,this);
    
    0 讨论(0)
  • 2020-12-07 17:11

    0

    After a long sacrifice i got this solution if you are using fragments then just use this code.

    getActivity().getSupportLoaderManager().initLoader(1, null, YourActivity.this);

    i hope this is helpful for you

    0 讨论(0)
  • 2020-12-07 17:12

    This is what you have:

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

    This is what you should have:

    LoaderManager.getInstance(this).initLoader(0, null, this);
    

    Reason for the first one not working:

    /**
     * @deprecated Use
     * {@link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
     */
    
    0 讨论(0)
  • 2020-12-07 17:13

    Change your imports to

    import android.support.v4.app.Fragment;
    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.Loader;
    

    and use

    getSupportLoaderManager().initLoader(0, null, this);
    
    0 讨论(0)
提交回复
热议问题