Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoaderManager with multiple loaders: how to get the right cursorloader

To me it's not clear how to get the right cursor if you have multiple Loaders. Lets say you define two different Loader with:

getLoaderManager().initLoader(0,null,this);
getLoaderManager().initLoader(1,null,this);

then in onCreateLoader() you do different things depending on the id:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {

    if (id==0){
               CursorLoader loader = new CursorLoader(getActivity(),
            MaterialContentProvider.CONTENT_URI,null,null,null,null);
    }else{
               CursorLoader loader = new CursorLoader(getActivity(),
            CustomerContentProvider.CONTENT_URI,null,null,null,null);
            };
    return loader;
} 

so far so good. But how to get the right cursor in onLoadFinished() because you don't get any id to identify the right Cursor for the right Cursoradapter.

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {


    mycursoradapter1.swapCursor(cursor);
    if(isResumed()){
        setListShown(true);
    }else {
        setListShownNoAnimation(true);
    }



}
//and where to get the cursor for mycursoradapter2

or am I wrong and this is the wrong way to get results for two different cursoradapter in one fragment.

like image 929
Kay Gladen Avatar asked Oct 09 '22 03:10

Kay Gladen


2 Answers

The Loader class has a method called getId(). I would hope this returns the id you've associated with the loader.

like image 121
Kurtis Nusbaum Avatar answered Oct 10 '22 15:10

Kurtis Nusbaum


Use the getId() method of Loader:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
        case 0:
            // do some stuff here
            break;
        case 1:
            // do some other stuff here
            break;
        case 2:
            // do some more stuff here
            break;
        default:
            break;
    }
}    
like image 32
IgorGanapolsky Avatar answered Oct 10 '22 16:10

IgorGanapolsky