Good Afternoon, I am developing an Android application that has ArrayLists of CustomObjects.
These objects are generated at run-time and their properties are set from SQLite data. Since I am using SQlite i will be requiring the use of some sort of AsyncTask, and I believe the AsyncTaskLoader fits the bill.
I want to create some sort of "Manager" class that will manage the creation/loading/updating of these custom objects (There are 3 custom object types). What i want to do is create the "Manager" class and have 3 inner classes that will extend AsyncTaskLoader (As well as other various functions as required).
My question to you is, How can my MainActivity work with these inner classes that are a part of the "Manager" class? How will MainActivity receive the callbacks? Is there a better solution than this?
Something like this...
public class ObjectManager implements LoaderManager.LoaderCallbacks<ArrayList<Object>>{
private Context mContext;
private LoaderManager mLoaderManager;
private ObjectManagerCallBacks mObjectManagerCallBacks;
public ObjectManager(Context context, LoaderManager loaderManager){
this.mContext = context;
this.mObjectManagerCallBacks = (ObjectManagerCallBacks) context;
this.mLoaderManager = loaderManager;
mLoaderManager.initLoader(*id#1*,null,this);
mLoaderManager.initLoader(*id#2*,null,this);
mLoaderManager.initLoader(*id#3*,null,this);
}
public interface ObjectManagerCallBacks{
void contentUpdated(int loader,ArrayList<Object> newData);
}
@Override
public Loader<ArrayList<Object>> onCreateLoader(int id, Bundle args) {
switch (id){
case #1:
return new #1Loader(mContext);
case #2:
return new #2Loader(mContext);
case #3:
return new #3Loader(mContext);
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<ArrayList<Object>> loader, ArrayList<Object> data) {
switch (loader.getId()){
case #1:
mObjectManagerCallbacks.contentUpdated(#1LOADER,data);
break;
case #2:
mObjectManagerCallbacks.contentUpdated(#2LOADER,data);
break;
case #3:
mObjectManagerCallbacks.contentUpdated(#3LOADER,data);
break;
default:
}
}
@Override
public void onLoaderReset(Loader<ArrayList<Object>> loader) {
switch (loader.getId()){
case WORKOUT_LOADER:
mObjectManagerCallbacks.contentUpdated(#1LOADER,null);
break;
case EXERCISE_LOADER:
mObjectManagerCallbacks.contentUpdated(#2LOADER,null);
break;
case USER_EXERCISE_LOADER:
mObjectManagerCallbacks.contentUpdated(#3LOADER,null);
break;
default:
}
}
//Various methods for object maintenance will go here
//And here
//And here...
private class LoaderTask1 extends AsyncTaskLoader<ArayList<CustomObject1>>{
}
private class LoaderTask2 extends AsyncTaskLoader<ArayList<CustomObject2>>{
}
private class LoaderTask3 extends AsyncTaskLoader<ArayList<CustomObject3>>{
}
}
Thank you all for your time.
My Solution that worked --
I ended up creating 3 inner classes in my CustomObjectManager class. These 3 classes extend AsyncTaskLoader. I followed a few guides on the task loader and managed to get them to load my cursor as well as create my objects asynchronously. I also created an interface to communicate these innerclasses with my MainActivity to notify ListViews that there is new data to be displayed.
I want to create some sort of "Manager" class that will manage the creation/loading/updating of these custom objects...
This is exactly the purpose of the LoaderManager
... to manage one or more loaders across a single activity/fragment instance. In my opinion, creating a separate ObjectManager
class whose only purpose is to serve as a wrapper around the loaders is only going to complicate your activity/fragment logic. Just have your Activity
implement the LoaderManager.LoaderCallbacks
interface and you'll achieve the exact same thing without the extra code.