28

I am currently trying to learn how to use Loaders and am having trouble starting a Loader in my activity.

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

public class ASwitchActivity extends Activity implements 
             LoaderManager.LoaderCallbacks<SampleLoader.SampleLoaderResult> {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getLoaderManager().initLoader(0, null, this);
    }

    public Loader<SampleLoader.SampleLoaderResult> onCreateLoader(int id, Bundle args) {
      return new SampleLoader(getBaseContext(), account, "dog");
}

  public void onLoadFinished(Loader<SampleLoader.SampleLoaderResult> loader, SampleLoader.SampleLoaderResult out)
  {
      TextView t=(TextView)findViewById(R.id.testTV);
      t.setText("yay");
  }

  public void onLoaderReset(Loader<SampleLoader.SampleLoaderResult> loader){

  }
}   

However Eclipse gives an error stating:

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

Can anyone help with where I am going wrong?

Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
d.mc2
  • 1,107
  • 3
  • 16
  • 29

4 Answers4

83

As I can see you use supportV4 library. So to implement Loader you should do some things:

  1. extend your activity from FragmentActivity class
  2. Use getSupportLoaderManager method instead of getLoaderManager

here is sample code:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.widget.Toast;

public class MyActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Object> {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<Object> onCreateLoader(int i, Bundle bundle){
    return null; // TODO
}

@Override
public void onLoadFinished(Loader loader, Object o) {
    Toast.makeText(this, "onLoadFinished", Toast.LENGTH_SHORT).show();
}

@Override
public void onLoaderReset(Loader loader)    {
    Toast.makeText(this, "onLoaderReset", Toast.LENGTH_SHORT).show();
}
}
mig35
  • 1,422
  • 11
  • 16
  • 1
    What if you originally extended `ListActivity`? There is no such thing as a `FragmentListActivity` that I can drop in as a replacement. – Matt Huggins May 27 '13 at 11:10
  • 2
    @MattHuggins ListActivity is mainly a convenience for very few things - you can implement the missing functionality yourself (keep ListView as field) – ataulm Dec 29 '13 at 02:40
11

When using loaders with fragments use:

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

And when using loaders with Activity use:

getSupportLoaderManager().initLoader(0,null,this);
Kurt Van den Branden
  • 9,082
  • 8
  • 60
  • 72
Krishna Agarwal
  • 131
  • 1
  • 7
1

For AppCompatActivity use getSupportLoaderManager().initLoader(0,null,this); for initializing the loader.

1

The third parameter for getLoaderManager().initLoader(0, null, this); should be a instance that implement interface LoaderManager.LoaderCallbacks So you should implement the interface first.

Lucifer
  • 28,605
  • 21
  • 86
  • 137
Changwei Yao
  • 12,641
  • 3
  • 21
  • 22
  • Sorry I didn't post my complete code earlier, only an excerpt. What do you mean by the LoaderManager.LoaderCallbacks interface? Would they be the onCreateLoader, onLoadFinished, and onLoaderReset? because I have them in my program? – d.mc2 Mar 29 '12 at 03:19