我的数据库表包含{Name,Time(UTC格式),Latitude,Longitude}
列我使用带有SimpleCursorAdapter的ListActivity显示表。
我希望列Time以人类可读的格式显示时间(13-07-2010 10:40),而不是UTC格式(18190109089)。
如何指定时间列中的值需要一些过滤/适应?
可能的解决方案(有问题):
SimpleCursorAdapter提供方法:
setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);
指定一个能够将Cursor转换为CharSequence的类(convertToString(Cursor cursor))。 无论如何,我不知道返回CharSequence参数的格式应该是哪种格式!
答案 0 :(得分:74)
格式化游标值的最简单方法是使用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;
}
});
答案 1 :(得分:13)
经过长时间的斗争,我也遇到了同样的问题,最后我找到了答案:)(见下文)
use setViewText (TextView v, String text)
例如
SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.somelayout, accountCursor, from, to)
{
@Override
public void setViewText(TextView v, String text) {
super.setViewText(v, convText(v, text));
}
};
private String convText(TextView v, String text)
{
switch (v.getId())
{
case R.id.date:
String formatedText = text;
//do format
return formatedText;
}
return text;
}
答案 2 :(得分:4)
您可以使用setViewBinder()
或子类SimpleCursorAdapter
并覆盖bindView()
。
答案 3 :(得分:4)
您可以使用该列的SQLite语法格式化日期。
这样的事情会做到这一点
SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');
SELECT strftime('%d-%m-%Y %H:%M',timecol,'unixepoch');
答案 4 :(得分:0)
通过这篇旧帖子,注意到我做了类似的事情可能有所帮助:
public class FormatCursorAdapter extends SimpleCursorAdapter {
protected int[] mFormats;
public static final int FORMAT_TEXT=0;
public static final int FORMAT_CURRENCY=1;
public static final int FORMAT_DATE=2;
public FormatCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int[] formats, int flags) {
super(context, layout, c, from, to, flags);
mFormats = formats;
ViewBinder viewBinder = new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int formatType = mFormats[columnIndex-1];
switch (formatType) {
case FORMAT_CURRENCY:
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setMaximumFractionDigits(2);
((TextView)view).setText(nf.format(cursor.getDouble(columnIndex)));
return true;
case FORMAT_DATE:
DateFormat df = SimpleDateFormat.getDateTimeInstance();
((TextView)view).setText(df.format(new Date(cursor.getLong(columnIndex))));
return true;
}
return false;
}
};
setViewBinder(viewBinder);
}
}
用法:
// For the cursor adapter, specify which columns go into which views with which format
String[] fromColumns = {
Table.COLUMN_TITLE,
Table.COLUMN_AMOUNT,
Table.COLUMN_DATE};
int[] toViews = {
R.id.tvTitle,
R.id.tvAmount,
R.id.tvDate};
int[] formatViews = {
FormatCursorAdapter.FORMAT_TEXT,
FormatCursorAdapter.FORMAT_CURRENCY,
FormatCursorAdapter.FORMAT_DATE};
mAdapter=new FormatCursorAdapter(getContext(),R.layout.item_operation,cursor,
fromOpsColumns,toOpsViews,formatViews,0);
mListView.setAdapter(mOpsAdapter);
希望这可以帮助那里的任何人!