使用SimpleCursorAdapter更改游标中的值

28

我有一个数据库表,包含列{Name,时间(UTC格式),纬度,经度}。

我使用ListActivity和SimpleCursorAdapter来显示表格。

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

我该如何指定需要对时间列的值进行某些过滤/适应?

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

SimpleCursorAdapter提供了以下方法:

setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter);

如何指定一个能够将Cursor转换为CharSequence的类(convertToString(Cursor cursor))的返回值格式是什么,我并不清楚应该使用哪种格式来返回CharSequence参数!

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;
    }
});

6
记住,aColumnIndex游标中可以找到数据的列。我使用了Cursor.getColumnIndex进行比较。 - theblang

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;
}

4
你可以在该列上使用 SQLite 语法来格式化日期。
像这样做就可以了。
SELECT strftime('%d-%m-%Y %H:%M',1092941466,'unixepoch');

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

4
您可以使用setViewBinder()或子类化SimpleCursorAdapter并重写bindView()方法。

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);

希望这能帮助到任何需要的人!

这个问题在6年前就已经被回答了...请解释一下你的代码带来了什么新价值? - Maciej Jureczko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接