1

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.

Nick H
  • 8,897
  • 9
  • 41
  • 64
  • ...has an ArrayList of ... ... ... ? – ElDuderino Jun 11 '14 at 15:44
  • 1
    just use a CursorLoader – pskink Jun 11 '14 at 15:49
  • Yup. Cursor loader is the way to go. – Shmuel Jun 11 '14 at 15:59
  • So just use a CursorLoader then build the objects inside that? – Nick H Jun 11 '14 at 16:46
  • @hitch.united what objects? your data source is sqlite db, so return Cursors and use [Simple]CursorAdapter – pskink Jun 12 '14 at 04:29
  • @pskink, The only reason im using SQL is to store the custom object properties so I can rebuild them when the application starts back up. I ended up going with AsyncTaskLoader to query in the background then create an array of custom objects out of it. Will update my post shortly for others. – Nick H Jun 12 '14 at 13:21
  • @hitch.united why custom interface? why not to use http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html ? – pskink Jun 12 '14 at 14:01
  • @pskink I am actually using that for my manager class with the 3 Asynctaskloaders, once onLoadFinished is called by them they use the custom interface to let my MainActivity know that the content needs to be refreshed. AFAIK this seems to be a good route, it is working pretty well so far. Let me update my pseudo code in my question to reflect where I am at with this. Thanks for all of your suggestions – Nick H Jun 12 '14 at 19:06

1 Answers1

0

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250