Format data between SQLite and ListView

会有一股神秘感。 提交于 2019-12-23 02:56:25

问题


I'm sure there must be a simple solution to this... I have a SQLite database, and I'm using the standard SimpleCursorAdaptor to put the database into a ListView. No problems there. However, I would like to be able to format some of the data in between the database and the ListView. For example, I want to divide all the data in the "price" column by 100 (in the database a price might be "5400", I want it displayed as "54.00").


回答1:


Use SimpleCursorAdapter.ViewBinder:

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(..);
simpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if(columnIndex == someColumnValue) {
                TextView text = (TextView) view;  // get your View
                text.setText(String.valueOf(cursor.getInt(1)/100));  //set some data
                return true;
        }
        return false;
    }
});


来源:https://stackoverflow.com/questions/5851543/format-data-between-sqlite-and-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!