programing tip

여러 로더가있는 LoaderManager : 올바른 커서 로더를 얻는 방법

itbloger 2020. 7. 20. 07:47
반응형

여러 로더가있는 LoaderManager : 올바른 커서 로더를 얻는 방법


나에게 로더가 여러 개인 경우 올바른 커서를 얻는 방법이 명확하지 않습니다. 다음과 같이 두 개의 다른 로더를 정의한다고 가정 해 보겠습니다.

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

그런 다음 onCreateLoader () 에서 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;
} 

여태까지는 그런대로 잘됐다. 그러나 올바른 Cursoradapter에 대한 올바른 커서를 식별하기 위해 id를 얻지 못하기 때문에 onLoadFinished () 에서 올바른 커서를 얻는 방법 .

@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

또는 내가 틀렸고 이것은 하나의 조각으로 두 개의 다른 커서 어댑터에 대한 결과를 얻는 잘못된 방법입니다.


Loader 클래스에는 getId () 라는 메소드가 있습니다. 로더와 관련된 ID를 반환하기를 바랍니다.


로더 getId () 메소드를 사용하십시오 .

@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;
    }
}    

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.

참고URL : https://stackoverflow.com/questions/7957418/loadermanager-with-multiple-loaders-how-to-get-the-right-cursorloader

반응형