Java – Android loadermanager confused with cursorloader

I tried to convert my Android application to use loadermanager and cursorloader Basically, I have a SQLite database with address column and distance column. I want to load the column values into the listview row

Now, I have done a lot of research, and all the content points to this tutorial: http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/

This is a good tutorial, but there are a few things I still don't understand Mainly, how to construct the content URI passed to 'new cursorloader()'? I don't use any external data, such as device contacts, etc

Refer to the code below I'm interested in how to build base_ Confused by URI generated values:

public class FavoritesFragment extends ListFragment implements
    LoaderManager.LoaderCallbacks<Cursor> {

SimpleCursorAdapter mAdapter;

static final String[] FAVORITES_SUMMARY_PROJECTION = new String[] {
        MyApplication.COLUMN_ID,MyApplication.COLUMN_ADDRESS,MyApplication.COLUMN_DISTANCE,};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAdapter = new SimpleCursorAdapter(getActivity(),R.layout.locations_list_row,null,new String[] {
                    MyApplication.COLUMN_ADDRESS,MyApplication.COLUMN_DISTANCE },new int[] {
                    R.id.address2,R.id.distance },0);
    setlistadapter(mAdapter);

    getLoaderManager().initLoader(0,this);
}

@Override
public void onListItemClick(ListView l,View v,int position,long id) {
    // Insert desired behavior here.
}

public Loader<Cursor> onCreateLoader(int id,Bundle args) {
    return new CursorLoader(getActivity(),***BASE_URI***,FAVORITES_SUMMARY_PROJECTION,null);
}

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

public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}

}

Solution

BASE_ The URI should be a static URI defined in the ContentProvider. It is used to set the correct query to the correct table in the database by using the integer statement of switch (in the example of the query() method in this tutorial) when you query / update / insert / perform any operation on the ContentProvider You should define a different base for each table in the database_ URI.

If you view this tutorial, they have defined a URI in the ContentProvider:

private static final String TUTORIALS_BASE_PATH = "tutorials";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + TUTORIALS_BASE_PATH);

In the content provider, you should set tutorials_ BASE_ The value of path is changed from "tutorials" to the name of the table containing the address and distance columns you mentioned Your cursorloader constructor code is as follows:

return new CursorLoader(getActivity(),YourContentProvider.CONTENT_URI,null);

For completeness, you should change the variable name to be more descriptive, so you should change it to something like locations_ BASE_ Path and locations_ Tutorials for URI_ BASE_ Path and content_ URI.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>