问题
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