Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a Loader in an Activity

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?

like image 384
d.mc2 Avatar asked Mar 29 '12 02:03

d.mc2


People also ask

How to loading data using Loader?

Loader API Summary To get LoaderManager, call getSupportLoaderManager() from the activity or fragment. To start loading data from a loader, call either initLoader() or restartLoader() .

What are loaders in Android?

Loaders are special purpose classes that manage loading and reloading updated data asynchronously in the background using AsyncTask. Introduced in Android 3.0, loaders have these characteristics: They are available to every Activity and Fragment. They provide asynchronous loading of data in the background.

What is loader app?

arrow_forward. LoaderDroid is a Download Manager tailor made for the Android platform. It supports ANY type of file for downloading: Videos, Images, Music, Applications, anything. And all this is accomplished whilst having a very small footprint on your Androids resources.


1 Answers

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(); } } 
like image 101
mig35 Avatar answered Sep 21 '22 11:09

mig35