Initializing a Loader in an Activity
Asked Answered
S

4

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?

Sally answered 29/3, 2012 at 2:39 Comment(0)
P
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();
}
}
Polarity answered 29/3, 2012 at 3:59 Comment(2)
What if you originally extended ListActivity? There is no such thing as a FragmentListActivity that I can drop in as a replacement.Boynton
@MattHuggins ListActivity is mainly a convenience for very few things - you can implement the missing functionality yourself (keep ListView as field)Plautus
A
11

When using loaders with fragments use:

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

And when using loaders with Activity use:

getSupportLoaderManager().initLoader(0,null,this);
Audette answered 8/11, 2016 at 10:25 Comment(0)
I
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.

Infest answered 29/3, 2012 at 3:8 Comment(1)
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?Sally
L
1

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

Legit answered 11/8, 2016 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.