public Loader <cursor> onCreateLoader(int id,Bundle args)方法调用多个CursorLoader

时间:2016-02-01 08:30:56

标签: android android-cursorloader

任务:我想显示ListView列表,当你选择一个项目来显示对话列表并将其显示在另一个元素列表中(父元素和子元素)

  static class GroupCursorLoader extends CursorLoader {
    DB db;

    public GroupCursorLoader(Context context, DB db) {
        super(context);
        this.db = db;
    }

    @Override
    public Cursor loadInBackground() {
        Cursor cursor = db.getGroupAll();

        return cursor;
    }
}


static class DetailCursorLoader extends CursorLoader{
    DB db;

    public DetailCursorLoader(Context context, DB db) {
        super(context);
        this.db = db;
    }

    @Override
    public Cursor loadInBackground() {
        Cursor cursor;
        cursor = db.getDetailAll();

        return cursor;
    }
}

现在,问题是,如何处理两个Cursor?,即。用以下方法编写

  

public Loader onCreateLoader(int id,Bundle args)

     

public void onLoadFinished(Loader loader,Cursor data)

     

public void onLoaderReset(Loader loader)

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

    return return new GroupCursorLoader(this, mDB);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    scAdapterForGroup.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    Log.d(LOG, "onLoadReset");
}

1 个答案:

答案 0 :(得分:0)

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
      if(id == GROUP_LOADER_ID) {
        return new GroupCursorLoader(this, mDB);       
      } else {
        return new DetailCursorLoader(this, mDB);
      }
    }

   @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
      if(loader instanceof GroupCursorLoader) {
        scAdapterForGroup.swapCursor(data);
      } else {
        scAdapterForDetails.swapCursor(data);
      }
   }

   @Override
   public void onLoaderReset(Loader<Cursor> loader) {
    Log.d(LOG, "onLoadReset");
   }

Answer question at ru.stackoverflow.com