LoaderManager with multiple loaders: how to get the right cursorloader

前端 未结 3 1327
南旧
南旧 2020-12-12 10:55

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(         


        
相关标签:
3条回答
  • 2020-12-12 11:07

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

    0 讨论(0)
  • 2020-12-12 11:16

    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;
        }
    }    
    
    0 讨论(0)
  • 2020-12-12 11:22

    If your loaders have nothing in common but the class type of the result (here: Cursor), you're better off creating two separate LoaderCallbacks instances (simply as two inner classes in your Activity/Fragment), each one dedicated to one loader treatment, rather than trying to mix apples with oranges.

    In your case it seems that both the data source and the result treatment are different, which requires you to write the extra boilerplate code to identify the current scenario and dispatch it to the appropriate code block.

    0 讨论(0)
提交回复
热议问题