I am doing some hands-on reading on AsyncTaskLoader so I can use the technique to load the contact list. The only time the code works is when I implement the callbacks from a class that extends Fragment as in MyLoader extends Fragment implements LoaderCallbacks<ArrayList<Contact>>. Is there another way? All I really need is the contact list (name, phone, thumbnail), to send to my backend. When, for example, I try to use Context, since I can get that from any activity by simply doing (Context)this, the code fails to even compile. By context I mean
context.getLoaderManager().initLoader(1, null, this);
//I already changed to Fragment so don't really remember the exact ".context" line.
//But someone who has done this will understand the snippet.
BTW: I am using multiple references. One is http://developer.android.com/reference/android/content/AsyncTaskLoader.html.
QUESTION (again): Can I use AsyncTaskLoader without Fragment or FragmentActivity?
THE CODE THAT WORKS WITH THE FRAGMENT:
package com.example.contactpreload.utils;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
public class LoadingContacts extends Fragment implements LoaderCallbacks<ArrayList<Contact>> {
ArrayList<Contact> loadedContacts;
static Fragment fragmentActivity;
public static LoadingContacts newInstance(int arg) {
LoadingContacts f = new LoadingContacts();
Bundle bundle = new Bundle();
bundle.putInt("index", arg);
f.setArguments(bundle);
fragmentActivity = new Fragment();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate()");
int mIndex = getArguments().getInt("index");
System.out.println(mIndex);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("onActivityCreated()");
getLoaderManager().initLoader(1, null, this);
}
#Override
public Loader<ArrayList<Contact>> onCreateLoader(int arg0, Bundle arg1) {
System.out.println("onCreateLoader()");
return new ContactsLoader(getActivity());
}
#Override
public void onLoadFinished(Loader<ArrayList<Contact>> loader, ArrayList<Contact> data) {
loadedContacts = data;
System.out.println("AND THE CONTACTS ARE: ");
for (Contact c : loadedContacts) {
System.out.println("NAME: " + c.getName());
System.out.println("getPhoneNumberHome: " + c.getPhoneNumber());
}
}
#Override
public void onLoaderReset(Loader<ArrayList<Contact>> arg0) {
System.out.println("onLoaderReset()");
// TODO Auto-generated method stub
}
}
package com.example.contactpreload;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.example.contactpreload.utils.LoadingContacts;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadingContacts fragment = LoadingContacts.newInstance(1);
fragment.setRetainInstance(true);
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MANIFEST:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
AsyncTaskLoader has nothing to do with whether you are using a Fragment or an Activity.
To give you an example, consider a list activity:
public class ExampleActivity extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todo_list);
//to start the loader:
getLoaderManager().initLoader(0, null, this);
}
//override the loader callback methods as usual
// Creates a new loader after the initLoader () call
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader = new CursorLoader(this,
uri, projection, null, null, null);
return cursorLoader;
}
#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);
}
}
Obviously you need to create corresponding adapter for the list view and layout, but the example just shows you how a simple loader for cursor would work for an Activity.
Also, make sure all your import are consistent, either using the support.v4 library, or the regular library:
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
Another question that is asked, is that if the LoaderCallback interface can be used standalone. This is not recommended if you consider that the LoaderCallback is for.
The LoaderManager.LoaderCallbacks<D> interface is a simple contract that the LoaderManager uses to report data back to the client. What that means, is its only job is to load some data in the background requested by a client, which is effectively, an activity.
If you create a standalone class, you could extend the class definition to implement LoaderManager.LoaderCallbacks<D>, but you will need to report the loaded data back to the original activity through some kind of mechanism, which would complicated a simple task.
Now if you are really fixed on doing this, you could create your standalone class as so:
public class LoadingContacts implements LoaderManager.LoaderCallbacks<Cursor> {
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return null;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
In your onLoadFinished method, you will need to send the loaded Cursor back through either a broadcast, or some kind of message bus:
LocalBroadcastManager
Otto
Messenger
After you send this information to the MainActivity, you can load it to the adapter and continue as is.
Related
I am working on a project which populating SQLite database table for list view and using simple array adapter.
I'm using Asyntask for that purpose and I have problem when:
I want to call another activity and
pass some values which I get from the setOnItemClickListener
I need to archive this two things in onPostExecute setOnItemClickListener method. This is my code for that.
package com.me.doctor.doctor_me;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<String,Doctor,String> {
Context ctx;
DoctorAdapter doctorAdapter;
Activity activity;
ListView listView;
Doctor doctor;
DisplayDoctor displayDoctor;
BackgroundTask(Context ctx){
this.ctx = ctx;
activity = (Activity) ctx;
doctor = new Doctor();
displayDoctor = new DisplayDoctor();
}
#Override
protected String doInBackground(String... strings) {
String method = strings[0];
DatabaseOperation databaseOperation = new DatabaseOperation(ctx);
if(method.equals("get_info")){
listView = activity.findViewById(R.id.display_list_view);
SQLiteDatabase db = databaseOperation.getReadableDatabase();
Cursor cursor = databaseOperation.getInformation(db);
doctorAdapter = new DoctorAdapter(ctx,R.layout.display_doctor_row);
String name, category, hospital;
int id;
while(cursor.moveToNext()){
id = cursor.getInt(cursor.getColumnIndex("d_id"));
name = cursor.getString(cursor.getColumnIndex("d_name"));
category = cursor.getString(cursor.getColumnIndex("d_category"));
hospital = cursor.getString(cursor.getColumnIndex("d_hospital"));
Doctor doctor = new Doctor(id,name,category,hospital);
publishProgress(doctor);
}
return "get_info";
}
return null;
}
#Override
protected void onProgressUpdate(Doctor... values) {
// add each of doctor class object add method inside the adapter class
doctorAdapter.add(values[0]);
}
#Override
protected void onPostExecute(String s) {
if(s.equals("get_info")){
listView.setAdapter(doctorAdapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Doctor doctor = (Doctor)parent.getItemAtPosition(position);
String ID = Integer.toString(doctor.getId());
Toast.makeText(ctx,ID,Toast.LENGTH_LONG).show();
// I need fire another activity and pass some values which i getting here
}
});
}else{
Toast.makeText(ctx,s,Toast.LENGTH_LONG).show();
}
}
}
And this is the class which call to the AsyncTask Class
package com.me.doctor.doctor_me;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class DisplayDoctor extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_doctor_layout);
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("get_info");
}
}
I had investigate closed question on Stack Overflow, but I did not found a solution.
Short answer:
you already have context in Background task
Context ctx;
use this this to call next activiry
ctx.startActivity(nextActivityIntent)
u can add values to the intent like this
Intent nextActivityIntent = new Intent(ctx,NextActivity.class);
nextActivityIntent.putExtra("data", "some data");
with async task i guess you are trying to query data base on another thread
You can use loaders for the same
Loaders run on separate thread
here is a simple example of cursor loader
example taken from github
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks {
public static final int LOADER_ID = 0;
private ArrayAdapter<String> forecastAdapter;
private ForecastAdapter mForecastAdapter;
public ForecastFragment() { }
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
getLoaderManager().initLoader(LOADER_ID, null, this);
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//some database query
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
//some action
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
How to send data to another fragment in one activity?
I have two fragment that have been created using Android Studio Design View Editor. I ccreated these two fragment on my MainActivity. fragment1 is the ID of first fragment, it contain just EditText and a button. fragment2 is the ID of second fragment, it just contain textView.
How to send data from EditText of fragment1 to textView of fragment2?
I have write some code below, please check it.
MainActivity.java
package com.example.radioswiba.belajar2buahfragment;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements Fragment1.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
Fragment1.java
//this code was generated by Android Studio
//i have deleted some unused code and comments
package com.example.radioswiba.belajar2buahfragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment1 extends Fragment {
//let's define some of variable
private EditText text_input;
private Button button_send;
private OnFragmentInteractionListener mListener;
public Fragment1() {
// Required empty public constructor
}
//this generated by Android Studio
public static Fragment1 newInstance(String param1, String param2) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
//this generated by Android Studio
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// my code here
View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
text_input = (EditText) rootView.findViewById(R.id.status_text);
button_send = (Button) rootView.findViewById(R.id.post_btn);
button_send.setOnClickListener(postStatus);
return rootView;
}
View.OnClickListener postStatus = new View.OnClickListener(){
#Override
public void onClick(View v){
text_of_me = text_input.getText().toString();
//
//WHAT SHOULD I WRITE HERE?
//SHOULD I USED BUNDLE?
}
};
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Fragment2.java
//the code almost same with Fragment1.java
I have search similar quenstion on stackoverflow, but i can not figure out. I have found many solution like below:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
There we create a new Fragment, meanwhile i have had two fragment on my activity, i have create it manually from file -> new -> new fragment from android Studio Menu. Should i create new Fragment by using above code?
A good way to communicate between fragments and/or activities is with Otto, an event bus library.
if you implement it correctly, I am quite confident it gonna solve your issue.
Here is few examples :
http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html
https://github.com/CardinalNow/event-bus-example
http://www.recursiverobot.com/post/48752686831/playing-around-with-otto-on-android
I've got this problem where when I change the orientation of my device, for some reason the on-screen representation of the ListView associated with my ListFragment is retained, and a new ListView is inflated beneath it. To give you some context, I am creating a very simple app whose purpose is to test some DAO objects I created. I would have preferred to post some screenshots depicting the behavior my app is experiencing, but unfortunately, my reputation is too low at the moment.
The best I could describe it would be that when I change the orientation of the device from portrait to landscape (and vice versa), it inflates a whole new ListView without deflating the previous one. I have been looking into this for a couple of days now, and quite honestly, I am stumped. Any help you guys could offer would be greatly appreciated.
Here is the code for the ListFragment:
package edu.uark.csce.mobile.healthyshopper;
import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
* A fragment representing a list of Items.
* <p />
* <p />
* Activities containing this fragment MUST implement the {#link Callbacks}
* interface.
*/
public class ViewFragment extends ListFragment implements
LoaderCallbacks<Cursor> {
// Class Fields:
private static final String TAG = "healthyshopper.ViewFragment";
private static final int MAIN_DB_LOADER = 0;
// In the final application, any query to the main database needs to return
// at least these fields:
private final String[] NECESSARY_COLUMNS = { DAOContentProvider.FOOD_DES,
DAOContentProvider.FOOD_CALORIES, DAOContentProvider.FOOD_FAT,
DAOContentProvider.FOOD_CARBS, DAOContentProvider.FOOD_PROTEIN };
private final String FROM[] = {DAOContentProvider.FOOD_GROUP, DAOContentProvider.FOOD_DES,
DAOContentProvider.FOOD_MANUFAC, DAOContentProvider.FOOD_PROTEIN,
DAOContentProvider.FOOD_FAT, DAOContentProvider.FOOD_CARBS,
DAOContentProvider.FOOD_CALORIES, DAOContentProvider.FOOD_SERV_SIZE};
private final int TO[] = {R.id.fd_group, R.id.shrt_desc,
R.id.manufac, R.id.protein,
R.id.fat, R.id.carb,
R.id.cal, R.id.amt};
private int threadMode;
private int queryType;
private int foodGroupId;
private CursorLoader loader;
private SimpleCursorAdapter listAdapter;
// Preference Option Constants:
private int UI_THREAD = 0;
private int BRANCHED_THREAD = 1;
private int ALL_ROWS = 0;
private int SPECIFIC_FD_GROUP = 1;
private int LIMITED_ROWS = 2;
private OnFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ViewFragment() {
}
/***************************************
* Life cycle callbacks:
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "in onCreate()");
setRetainInstance(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(savedInstanceState == null){
listAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_node, null, FROM, TO, 0);
setListAdapter(listAdapter);
}
}
#Override
public void onStart(){
super.onStart();
}
#Override
public void onResume(){
super.onResume();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()
.getApplicationContext());
Log.d(TAG, preferences.getString("THREADING_MODE", "BLANK"));
//Load testing preferences:
threadMode = Integer.valueOf(preferences.getString("THREADING_MODE","0"));
queryType = Integer.valueOf(preferences.getString("QUERY_TYPE", "0"));
foodGroupId = Integer.valueOf(preferences.getString("FOOD_GROUP", "0")); // Default
if (threadMode == BRANCHED_THREAD) {
loader = (CursorLoader) getLoaderManager().initLoader(
MAIN_DB_LOADER, null, this);
} else {
String[] projection = null;
String selection = null;
if (queryType == SPECIFIC_FD_GROUP) {
Log.d(TAG, "Query Type: " + (getResources().getStringArray(R.array.query_type_select))[queryType]);
selection = DAOContentProvider.FOOD_GROUP + " = " + foodGroupId;
} else if (queryType == LIMITED_ROWS) {
projection = NECESSARY_COLUMNS;
}
/*This is for testing only. Ordinarily, you should NEVER perform Db operations on the UI thread.*/
listAdapter.swapCursor(getActivity().getContentResolver().query(
DAOContentProvider.CONTENT_URI, projection, selection, null, null));
}
}
#Override
public void onPause(){
super.onPause();
}
#Override
public void onStop(){
super.onStop();
}
#Override
public void onDestroyView(){
super.onDestroyView();
Log.e(TAG, "in onDestroyView()");
setListAdapter(null);
listAdapter.swapCursor(null);
listAdapter = null;
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
public void onDetach(){
super.onDetach();
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (null != mListener) {
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated to
* the activity and potentially other fragments contained in that activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(String id);
}
/**
* LoaderManager Callbacks;
*/
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case MAIN_DB_LOADER:
String[] projection = null;
String selection = null;
if (queryType == SPECIFIC_FD_GROUP) {
Log.d(TAG,
"Query Type: "
+ (getResources()
.getStringArray(R.array.query_type_select))[queryType]);
selection = DAOContentProvider.FOOD_GROUP + " = " + foodGroupId;
} else if (queryType == LIMITED_ROWS) {
projection = NECESSARY_COLUMNS;
}
return new CursorLoader(getActivity(),
DAOContentProvider.CONTENT_URI, projection, selection,
null, null);
default:
return null;
}
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
listAdapter.swapCursor(arg1);
listAdapter.notifyDataSetChanged();
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
listAdapter.swapCursor(null);
listAdapter.notifyDataSetChanged();
}
}
And here is the code for its associated Activity:
package edu.uark.csce.mobile.healthyshopper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class TestingActivity extends Activity implements ViewFragment.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing);
ViewFragment list = new ViewFragment();
getFragmentManager().beginTransaction().add(R.id.test_layout, list).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testing, menu);
return true;
}
#Override
public void onFragmentInteraction(String id) {
// TODO Auto-generated method stub
}
}
You're using setRetainInstance(true); in your fragment. This makes the fragment to be retained and reused when the activity is re-created.
You can read more detailed description in this question: Further understanding setRetainInstance(true)
I am using an SQLite database and wish to load it without using a ContentProvider.
I am having trouble getting my subclassed SimpleCursorLoader (taken from CursorLoader usage without ContentProvider) to work with the LoaderManager.
In the overwritten method
#Override
public Loader<Cursor> onCreateLoader(int ID, Bundle args) {
return new ListCursorLoader(this, dBHelper);
}
I get a type mismatch saying that it cannot convert from ListCursorLoader to Loader<Cursor>. I have tried creating the ListCursorLoader on the fly (that is, in the method), but this does not work either.
Here is the code for my ListCursorLoader:
package utilities;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
public class ListCursorLoader extends SimpleCursorLoader {
private DBAdapter dBAdapter;
public ListCursorLoader(Context context, DBAdapter adapter) {
super(context);
dBAdapter = adapter;
}
#Override
public Cursor loadInBackground() {
Cursor cursor = null;
dBAdapter.open();
try {
cursor = dBAdapter.getAllQueries();
} catch (SQLException e) {
e.printStackTrace();
}
if (cursor != null) {
cursor.getCount();
}
return cursor;
}
}
As you can see I have only overwritten the loadInBackground() method, and I simply cannot see what I am doing wrong.
Hope you guys can help!
I just tried your code and it worked without an issue.
You should double-check your imports. The SimpleCursorLoader you linked to is using the support library. You didn't provide the code, but I think you may be using the default LoaderManager, not the one provided by the support library.
So for you to be able to use this class you need to reference android.support.v4.content.Loader and load it using the SupportLoaderManager in your Fragment.
Here is the code from my Fragment that worked:
(Note the use of the support library.)
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
...
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getSupportLoaderManager().initLoader(0, null, this);
}
...
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new ListCursorLoader(this);
}
...
}
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?
As I can see you use supportV4 library.
So to implement Loader you should do some things:
extend your activity from FragmentActivity class
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();
}
}
When using loaders with fragments use:
getLoaderManager().initLoader(0,null,this);
And when using loaders with Activity use:
getSupportLoaderManager().initLoader(0,null,this);
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.
For AppCompatActivity use getSupportLoaderManager().initLoader(0,null,this); for initializing the loader.