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?
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() .
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.
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.
As I can see you use supportV4 library. So to implement Loader you should do some things:
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With