27

我有列 {Name, Time (UTC format) , Latitude, Longitude} 的数据库表

我使用带有 SimpleCursorAdapter 的 ListActivity 显示表格。

我希望时间列以人类可读的格式(13-07-2010 10:40)而不是 UTC 格式(18190109089)显示时间。

如何指定时间列中的值需要一些过滤/调整?

可能的解决方案(有问题):

SimpleCursorAdapter 提供了方法:

setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);

指定如何将 Cursor 转换为 CharSequence (convertToString(Cursor cursor) 的类。无论如何我不知道返回 CharSequence 参数应该采用哪种格式!

4

5 回答 5

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;
    }
});
于 2011-01-23T14:42:41.317 回答
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;
}
于 2011-01-04T20:39:22.510 回答
4

您可以使用setViewBinder(), 或子类化SimpleCursorAdapter并覆盖bindView().

于 2010-08-31T13:41:56.723 回答
4

您可以在该列上使用SQLite语法来格式化日期。

这样的事情会做到

SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');

SELECT strftime('%d-%m-%Y %H:%M',timecol,'unixepoch');
于 2010-08-31T13:47:03.483 回答
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);

希望这可以帮助那里的任何人!

于 2017-09-01T18:12:46.043 回答