如何使用cursorloader从查询中获取游标

时间:2015-03-13 15:04:12

标签: android

嗨我想从扩展内容提供商的活动中获取光标  当我试图在主要活动中得到它 我需要给:

Cursor cursor = theprovider.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

我不明白在查询中要提供什么

这是主要活动:

 public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> {

SimpleCursorAdapter adapter;

private TheProvider theprovider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    theprovider = new TheProvider();


   Cursor cursor = theprovider.query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

    startManagingCursor(cursor);

    String [] from = {ConContract.PlacesCons.NAME,ConContract.PlacesCons.ADDRESS,
            ConContract.PlacesCons.DISTANCE};

    int[] to = {R.id.title_name,R.id.title_address,R.id.title_distance};

    adapter = new SimpleCursorAdapter(this,R.layout.abc_list_menu_item_layout,cursor,
            from,to,0);

   ListView loc_list = (ListView) findViewById(R.id.Loc_list);

    loc_list.setAdapter(adapter);

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

    if (isInSingleFragment()) {

        if (savedInstanceState == null) {

            FragmentA fragmentA = new FragmentA();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragmnetContainer,fragmentA,"A");
            ft.commit();

        }
    } else {

        if (savedInstanceState == null) {

            FragmentA fragmentA = new FragmentA();
            FragmentB fragmentB = new FragmentB();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragmnetContainerA, fragmentA, "A");
            ft.add(R.id.fragmnetContainerB, fragmentB, "B");
            ft.commit();

        }

    }
}

private boolean isInSingleFragment() {

    View layout = findViewById(R.id.layout_singleLayout);

    if(layout!=null){
        return true;
    }

    else{
        return  false;

    }

}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
     Uri uri = ConContract.PlacesCons.CONTENT_URI;

    String[] projection = {
            ConContract.PlacesCons.NAME,
            ConContract.PlacesCons.ADDRESS,
            ConContract.PlacesCons.DISTANCE
    };

    String selection = null;

    String sortOrder = ConContract.PlacesCons.NAME + " desc" ;

    return new CursorLoader(this,uri,projection,selection,null,sortOrder);


}

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

    adapter.swapCursor(cursor);
}


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

    adapter.swapCursor(null);

}

}

0 个答案:

没有答案