【发布时间】:2016-12-08 09:40:40
【问题描述】:
我的数据库中有多个具有三个字符串属性的对象。其中一个显示日期信息,例如。 2016 年 1 月 4 日,2017 年 10 月 20 日。我在 ListView 中列出了这些对象,但在我想按日期对它们进行排序之前。
//
//Show all Data on ListView
//
private void displayListView() {
Cursor cursor = datasource.fetchAllData();
// The desired columns to be bound
String[] columns = {
DatabaseHelper.COLUMN_TITLE,
DatabaseHelper.COLUMN_DESCRIPTION,
DatabaseHelper.COLUMN_DEADLINE,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.processobject_title,
R.id.processobject_description,
R.id.processobject_deadline,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.listview_viewlayout,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listview);
ArrayList <ProcessObject> arrayList = new ArrayList<>();
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
activateAddButton();
OnItemClickListener();
};
我看到了两个机会:对光标进行排序,或者将数组排序为 []。但我不知道怎么做。
public Cursor fetchAllData(){
Cursor cursor = database.query(DatabaseHelper.TABLE_DATABASE, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
【问题讨论】:
-
以“YYYY-MM-DD”格式存储日期,以便您可以在查询中使用
ORDER BY,或者将日期存储为INTEGER,并在需要时进行转换。 Sqlite Date and Time Datatype -
我这样做了,现在我将日期作为“yyyy-MM-dd”存储在我的数据库中。但我不知道如何准确应用 ORDER BY。可以举个例子吗?
标签: android arrays sorting listview cursor