android – 如何对CursorLoader结果进行排序?

前端之家收集整理的这篇文章主要介绍了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,this);

创建加载器:

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

    return new CursorLoader(getActivity(),Uri.withAppendedPath(TraceTable.CONTENT_URI,"latest"),MEMBERS_PROJECTION,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);
    }
}

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

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

解决方法

这实际上非常简单:

由此:

new CursorLoader(getActivity(),null);

对此:

// 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(),projection,"data ASC");

请记住,如果您的提供商中有一些方法可以检查投射并引发异常,您必须在进行测试时将其注释掉,或者将新列(您计算的那个)添加到官方投影数组中.

原文链接:https://www.f2er.com/android/315826.html

猜你在找的Android相关文章