使用SimpleCursorAdapter从游标更改值

我有数据库表的列{名称,时间(UTC格式),纬度,经度}

我用SimpleCursorAdapter使用ListActivity显示表。

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

我如何指定来自列时间的值需要一些过滤/适应?

可能的解决scheme(有问题):

SimpleCursorAdapter提供了这个方法:

setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter); 

指定一个能够将一个Cursor转换为CharSequence的类(convertToString(Cursor cursor))。不pipe怎样,我不知道应该以哪种格式返回CharSequence参数!

Solutions Collecting From Web of "使用SimpleCursorAdapter从游标更改值"


格式化游标值最简单的方法是使用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; } }); 

经过漫长的斗争,我也遇到了同样的问题,最后我find了答案:)(见下文)

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

您可以使用setViewBinder()SimpleCursorAdapter子类并覆盖bindView()

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

像这样的事情会做到这一点

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

通过这个旧post,注意到我做了类似的事情可能会有所帮助:

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

希望这有助于任何人在那里!