I use the following SimpleCursorAdapter:
String campos[] = { "nome_prod", "codbar_prod",
"marca_prod", "formato_prod", "preco"};
int textviews[] = { R.id.textProdName, R.id.textProdCodBar, R.id.textProdMarca,
R.id.textProdFormato, R.id.textProdPreco };
CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.listview,
c_list, campos, textviews, 0);
And this works fine. But "preco" from "campos[]" comes from a double value. Can I format this somehow so my cursor (which feeds a listview) will display this double with two digits after the point (like a money value)?
Can I do it in some simple way, like using "%.2f" somewhere, or will I have to subclass CursorAdapter?
Thanks in advance.
You don't need to subclass CursorAdapter. Simply create a ViewBinder and attach it to the adapter, it will transform the value of a specific column of the cursor. Like so:
dataSource.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 5) {
Double preco = cursor.getDouble(columnIndex);
TextView textView = (TextView) view;
textView.setText(String.format("%.2f", preco));
return true;
}
return false;
}
});