使用 SimpleCursorAdapter 从 Cursor 更改值 [英] Changing values from Cursor using SimpleCursorAdapter
问题描述
我有包含 {Name, Time (UTC format) , Latitude, Longitude} 列的数据库表
I have database table with the columns {Name, Time (UTC format) , Latitude, Longitude}
我使用带有 SimpleCursorAdapter 的 ListActivity 显示表格.
I display the table using a ListActivity with a SimpleCursorAdapter.
我希望 Time 列以人类可读的格式 (13-07-2010 10:40) 而不是 UTC 格式 (18190109089) 显示时间.
I would like that the column Time show the time in a human readable format (13-07-2010 10:40) rather than in UTC format (18190109089).
如何指定 Time 列中的值需要进行一些过滤/调整?
How can I specify that the values from column Time need some filtering/adaptation?
可能的解决方案(有问题):
POSSIBLE SOLUTION (with a problem):
SimpleCursorAdapter 提供方法:
SimpleCursorAdapter offers the method:
setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);
指定能够将 Cursor 转换为 CharSequence (convertToString(Cursor cursor) 的类的方式.反正我不知道返回的CharSequence参数应该是哪种格式!
to specify how a class that is able to convert a Cursor to CharSequence (convertToString(Cursor cursor). Anyway I don't know in which format should be the return CharSequence paramater!
推荐答案
格式化游标值的最简单方法是使用 SimpleCursorAdapter.setViewBinder(..):
The simplest way to format a cursor value is to use SimpleCursorAdapter.setViewBinder(..):
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor,
new String[] { Definition.Item.TITLE, Definition.Item.CREATE_DATE }, new int[] { R.id.title, R.id.createDate});
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
if (aColumnIndex == 2) {
String createDate = aCursor.getString(aColumnIndex);
TextView textView = (TextView) aView;
textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
return true;
}
return false;
}
});
这篇关于使用 SimpleCursorAdapter 从 Cursor 更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!