Set multiple cursor loaders with multiple adapters - Android

≡放荡痞女 提交于 2020-01-01 23:13:14

问题


I want to have two cursor loaders in my application fragment. Each one of them has different set of data and is used in different lists.

I found somewhere that with cursor you can use getId() method and then using switch do something. But, there is always method: getLoaderManager().initLoader(0,null,this); after which can be something like this:

adapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, null, from, to, 0);

This method is only for one cursor, but what if I have adapter1 and adapter2? How can I determine which cursor is for which adapter?


回答1:


My solution is to implement callback listeners

public class Query extends  FragmentActivity {//do not implement callbacks
getSupportLoaderManager().initLoader(Constants.LOADER_DAILY_TASK,
null,dailyTaskResultLoaderListener);//note listener not "this"
getSupportLoaderManager().initLoader(Constants.LOADER_OTHER_TASK,
null,otherTaskResultLoaderListener);//note listener not "this"

private LoaderCallbacks<Cursor> dailyTaskResultLoaderListener
= new LoaderCallbacks<Cursor>() {   
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bun) {     
        return  new CursorLoader(getBaseContext(),
Constants.DAILY_TASK_URI,projection, selection,selArgs,null); }
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) 
{...}
public void onLoaderReset(Loader<Cursor> cursor) {...}};
private LoaderCallbacks<Cursor> otherTaskResultLoaderListener
= new LoaderCallbacks<Cursor>() {       

....I think you get the idea




回答2:


Having separate loader callbacks is not necessary. The answer lies in the ID passed to the loader manager:

private static final int LOADER_A_ID = 1;
private static final int LOADER_B_ID = 2;

public void onCreate(Bundle savedInstanceState) {
    ...

    getLoaderManager().initLoader(LOADER_A_ID, null, this);
    getLoaderManager().initLoader(LOADER_B_ID, null, this);
}

public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    switch (loaderId) {
        case LOADER_A_ID:
            // return loader for cursor A/ adapter A

        case LOADER_B_ID:
            // return loader for cursor B/ adapter B
    }
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
        case LOADER_A_ID:
            adapterA.swapCursor(cursor);
            break;

        case LOADER_B_ID:
            adapterB.swapCursor(cursor);
            break;

    }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    ...
}

You can make as many cursor loaders as you like in this way and only implement the callback methods once.




回答3:


One possible solution is this. Remove the LoaderCallbacks interface from your Fragment declaration, then create two separate implementations of LoaderCallbacks, one for each adapter that you want to set up. Finally in the onLoadFinished method of each implementation set up the adapters.

public class ExampleFragmen extends Fragment { // Don't implement LoaderCallbacks here.

    private static final int LOADER_A = 0;
    private static final int LOADER_B = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        getLoaderManager().restartLoader(LOADER_A, null, new LoaderACallbacks());
        getLoaderManager().restartLoader(LOADER_B, null, new LoaderBCallbacks());
        ...
    }

    public class LoaderACallbacks implements LoaderCallbacks<Cursor> {

        @Override
        public Loader<Cursor> onCreateLoader(int loader, Bundle args) {
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            // Set up adapter A here...
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
        }

    }

    public class LoaderBCallbacks implements LoaderCallbacks<Cursor> {

        @Override
        public Loader<Cursor> onCreateLoader(int loader, Bundle args) {
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            // Set up adapter B here...
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
        }

    }

}


来源:https://stackoverflow.com/questions/22888233/set-multiple-cursor-loaders-with-multiple-adapters-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!