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