如何对CursorLoader结果进行排序?

Ale*_*han 7 android simplecursoradapter android-cursorloader

我使用CursorLoader查询结果,这不是我想在ListFramgenet中显示的顺序.怎么排序呢?

我用它来设置适配器:

    mAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_2, null,
            new String[] { "name", "distance"},
            new int[] { android.R.id.text1, android.R.id.text2 }, 0);
    setListAdapter(mAdapter);

    // Start out with a progress indicator.
    setListShown(false);

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader(0, null,  this);
Run Code Online (Sandbox Code Playgroud)

创建加载器:

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

    return new CursorLoader(getActivity(), 
            Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
            MEMBERS_PROJECTION,
            null,
            null,
            null);        

}


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

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

嗯,查询结果有纬度和经度.我想计算我的位置和这些结果之间的距离.并按距离asc排序.

怎么排序呢?任何答案都会被批评

fau*_*r21 13

这实际上非常简单:

由此:

new CursorLoader(getActivity(), 
        Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
        MEMBERS_PROJECTION,
        null,
        null,
        null);
Run Code Online (Sandbox Code Playgroud)

对此:

// You could have them calculated in the projection like this:
String[] projection = { COLUMN1 + " * " + COLUMN2 + " as data", // Whatever your calculations are
COLUMN1, COLUMN2, COLUMN3, etc..  };
new CursorLoader(getActivity(), 
        Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
        projection, 
        null,
        null,
        "data ASC");
Run Code Online (Sandbox Code Playgroud)

请记住,如果您的提供程序中有一些方法可以检查投影并引发异常,那么您必须在进行测试时添加注释或添加新列 (您使用)到您的官方投影阵列.