我在这里阅读了一些关于如何使用例如 collections.sort 或 sortOrder 按字母顺序排列列表视图的主题,但我无法让它在我的情况下工作,因为我没有使用数组列表。
我的列表视图是用数据库中的元素填充的,首先放入游标中,这样:
private void displayListView() {
// getExtra
Bundle bundle = getIntent().getExtras();
String title = bundle.getString("title", "Choose here :");
String inInterval = bundle.getString("inInterval");
Log.d(TAG, "inInterval = " + inInterval);
poititle.setText(title);
// put the results of the method in a cursor
c = dbHelper.findPoiInTable(inInterval);
displayCursor();
}
private void displayCursor() {
String[] columns = new String[] { DatabaseAdapter.COL_NAME,
DatabaseAdapter.COL_STREET, DatabaseAdapter.COL_WEBSITE,
DatabaseAdapter.COL_TELEPHONE, DatabaseAdapter.COL_REMARKS,
DatabaseAdapter.COL_PRICE, DatabaseAdapter.COL_MOBILE };
int[] to = new int[] { R.id.name, R.id.street, R.id.website,
R.id.telephone, R.id.remarks, R.id.price, R.id.mobile };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
columns, to, 0);
ListView listView = (ListView) findViewById(R.id.poilistview);
// Assign adapter to ListView
listView.setAdapter(cursorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// Comportement des éléments de la listview
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getApplicationContext(),
POIActivity.class);
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String street = ((TextView) view.findViewById(R.id.street))
.getText().toString();
String website = ((TextView) view.findViewById(R.id.website))
.getText().toString();
String telephone = ((TextView) view
.findViewById(R.id.telephone)).getText().toString();
String remarks = ((TextView) view.findViewById(R.id.remarks))
.getText().toString();
String price = ((TextView) view.findViewById(R.id.price))
.getText().toString();
String mobile = ((TextView) view.findViewById(R.id.mobile))
.getText().toString();
// i.putExtra(ID_EXTRA, name) ;
i.putExtra(ID_NAME, name);
i.putExtra(ID_STREET, street);
i.putExtra(ID_WEBSITE, website);
i.putExtra(ID_TELEPHONE, telephone);
i.putExtra(ID_REMARKS, remarks);
i.putExtra(ID_PRICE, price);
i.putExtra(ID_MOBILE, mobile);
startActivity(i);
}
});
int numberResults = listView.getCount();
listviewCount.setText("( " + numberResults + " results ) : ");
}
我正在寻找最简单的方法来指定我希望结果与单词 (name / ID_NAME) 一起按字母顺序显示。所以我的问题是,有没有办法添加一行:
排序顺序(名称,byAlph)