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

后端 未结 19 1576
无人共我
无人共我 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:03

    I'm using ActionBarSherlock with my app and I as well was running into this issue and worked through all the steps discussed by others in this question. However I was continuing to have the same problem after trying all the suggested resolutions.

    The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, this)
    

    The issue was I had fallen into expecting Eclipse to tell me when I was missing something and it was telling me that but not in the way I was used to. Typically Eclipse in other cases would tell me I'm missing the overrides to make something work but it's not directly saying that here. I finally picked up on the "LoaderManager.LoaderCallbacks" being the issue and realized I had no callbacks for it thus this error was actually a very valid error. Adding the basic overrides resolved my issue and allowed me to move forward.

    // Creates a new loader after the initLoader () call
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
      // do work
      return null;
    }
    
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
      adapter.swapCursor(data);
    }
    
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
      // data is not available anymore, delete reference
      adapter.swapCursor(null);
    }
    
    0 讨论(0)
  • 2020-12-07 17:04

    I got around this by using a SherlockListFragment (or you could use ListFragment, I suppose), but have it contained within a Activity. I first created a generic FragmentHolderActivity class that looks like this:

    FragmentHolderActivity.java

    public class FragmentHolderActivity extends SherlockFragmentActivity {
    
        public static final String FRAGMENT_LAYOUT_RESOURCE_ID = "fragment_layout_resource_id";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Integer fragmentLayoutResourceId = getIntent().getIntExtra(FRAGMENT_LAYOUT_RESOURCE_ID, Integer.MAX_VALUE);
            Assert.assertNotSame(fragmentLayoutResourceId, Integer.MAX_VALUE);
    
            setContentView(fragmentLayoutResourceId);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    finish();
                    return false;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    
    }
    

    Then you need to create an XML file for your fragment.

    your_list_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.example.YourListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment" />
    

    And here's the code you use to start the fragment:

        Intent intent = new Intent();
        intent.setClass(this, FragmentHolderActivity.class);
        intent.putExtra(FragmentHolderActivity.FRAGMENT_LAYOUT_RESOURCE_ID, R.layout.your_list_fragment);
        startActivity(intent);
    

    You just tell the FragmentHolderActivity to use the your_list_fragment layout, which in turn loads the YourListFragment.java

    You can then use: getSherlockActivity().getSupportLoaderManager().initLoader(...) in YourListFragment.java

    Not sure if this is the correct approach, but it keeps all my logic in Fragments, which is nice.

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

    It's late but maybe help some one.
    if you use loader maneger in fragment and you min api is below HONEYCOMB
    you shuld use this imports

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

    and for initiate loader use this code

    getActivity().getSupportLoaderManager().initLoader(/loader stuff*/);
    

    hope this help some one.

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

    If you use API 28 or later, instead of getLoaderManager use:

    getSupportLoaderManager().initLoader(id, null, this);
    
    0 讨论(0)
  • 2020-12-07 17:06

    Casting the third argument solved the problem in my case:

    from

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

    to

     getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);
    

    Note:

    1. minSdk was 8 and i was using support library v4.
    2. (android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this) did not work.
    3. getSupportLoaderManager() or getSupportLoadManager() did not work.
    4. This code was inside activity not fragment
    0 讨论(0)
  • 2020-12-07 17:06

    In my case I had to have my Activity extend ActionBarActivity from the android.support.v7.app package. I was then able to use

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

    0 讨论(0)
提交回复
热议问题