android - 使用 SimpleCursorAdapter 从 Cursor 更改值

标签 android sqlite filtering android-cursor

我有一个包含 {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 参数应该采用哪种格式!

最佳答案


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

关于android - 使用 SimpleCursorAdapter 从 Cursor 更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609126/

相关文章:

javascript - 从返回空数组的 API 中过滤对象数组

javascript - 如何使用 phonegap 按字母顺序排列从 android 设备获取的联系人?

sql - 是否可以使用当前行的非空单元格的数量来更新列?

android - 从 SQLite 迁移到 Android Room Persistence Library

java - Android 中最接近 Session/Cookie 变量的是什么?

python - 删除排列多索引的重复项

android - 如何构建 ibeacon 后端面板?

android - 启用 Proguard 时解码时找不到类

android - 什么是 ConstraintLayout 优化器?

Python高效过滤dict的方法