[英]Android Populate ListView from database, format textview with two decimals
我有这种方法从数据库填充ListView。 一切正常。 但是我想让文本字段之一显示带有两个小数的值。
之前已经问过这个问题,许多标记为重复的问题,但是问题是如何实现两个小数,以及如何在下面的代码中执行
如您所见,我尝试了许多不同的尝试,但均未成功。 也许我需要在设置TextViews的地方构建自定义适配器?
并且请不要将其标记为重复项,因为此处未给出etc的答案:
或在这里:
或在这里:
我的代码到了,我的问题是我可以在哪里实现我的代码以舍入到两位小数?
private void populateListViewFromDB(int month, int year) {
Cursor cursor = dbHandler.getMonth(month,year);
// TextView setEx = (TextView)findViewById(R.id.oneRowExkl);
// set = Double.parseDouble(setEx.getText().toString());
// setEx.setText(String.format("%.2f", set));
//TextView ex = (TextView)findViewById(R.id.oneRowExkl);
//ex.setText(String.format("%.2f", Double.parseDouble(MyDBHandler.INKL)));
String[] fromFieldNames = new String[] {
MyDBHandler.COLUMN_PRODUCTNAME, MyDBHandler.DATE,
MyDBHandler.INKL };
int[] toViewIDs = new int[] { R.id.oneRowName, R.id.oneRowDate,
R.id.oneRowExkl };
// Create adapter to may columns of the DB onto element in the UI.
@SuppressWarnings("deprecation")
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, //
Context
R.layout.one_row, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);
ListView myList = (ListView) findViewById(R.id.list1);
// myList.setBackgroundColor(Color.GRAY);
myList.setAdapter(myCursorAdapter);
}
首先,数据库中的数据类型是什么? 是否设置为处理小数/存储在数据库中? 您可以在数据库端更改舍入规则吗?
如果不是,则要实现四舍五入代码。 由于在示例中使用的是SimpleCursorAdapter,因此它使用convertToString(Cursor)将数据输出到视图。
您可以做的就是为它创建一个自定义的字符串转换器,该转换器针对指定的列创建,可以在其中进行舍入。
例如:
CursorToStringConverter myConverter = new CursorToStringConverter() {
@Override
public CharSequence convertToString(Cursor cursor) {
int myColumn = [whatever column id];
String s = cursor.getString(myColumn); //Or .getFloat() for instance
return [whatever rounding you want, or truncating, or such]
}
};
然后在适配器上进行设置:
myAdapter.setCursorToStringConverter(myConverter);
您还可以尝试为适配器提供一个新的ViewBinder,在其中检查所需的列,并在将其绑定时,在绑定之前手动编辑输出,像这样(在将适配器分配给视图之前将其粘贴) :
myAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == 3) {
//If it is actually stored as float number in db, do
//cursor.getFloat(columnIndex) instead
String formatStr = cursor.getString(columnIndex);
TextView textView = (TextView) view;
//Here, do your truncating
[insert whatever code to either truncate string or format float]
textView.setText(fixedStr);
return true;
}
return false;
}
});
为了只看它是如何工作的,在进行截断或格式化之前,请说一下[插入...]的位置:
String fixedStr = "Random string";
您可能会看到它的作用。
只是为了展示如何使用@Richar Erikssons代码解决问题,以下是解决我的问题的代码
myCursorAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int
columnIndex) {
if (columnIndex == 3) {
TextView textView = (TextView) view;
String fixedStr = String.valueOf(String.format("%.2f",
cursor.getDouble(columnIndex)));
textView.setText(fixedStr);
return true;
}
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.