【发布时间】:2014-01-08 07:12:04
【问题描述】:
我使用 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);
创建加载器:
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);
}
}
嗯,查询结果有经纬度。我想计算我的位置和这些结果之间的距离。并按距离 asc 排序。
如何排序?任何答案都会被应用
【问题讨论】:
-
你不能对已经返回的游标进行排序......你必须告诉 CursorLoader 在创建时为你排序 CursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
-
看来sortOrder只是一个字符串,我要计算的最终顺序。
-
您可以通过
(x-x0)^2 + (y-y0)^2订购...您也可以退货(x-x0)^2 + (y-y0)^2 AS Distance²(当然,您必须修改ContentProvider)...然后在显示之前您可以在Binder.bindView中计算它() 或 getView() 或类似...someView.setText(Math.sqrt(c.getDouble(c.getColumnIndex("Distance²"))).toString()) -
显然你需要阅读一些 SQLite 文档:sqlite.org/syntaxdiagrams.html#ordering-term
标签: android simplecursoradapter android-cursorloader