使用SimpleAdapter在TextView中显示浮点值

时间:2014-03-11 06:48:07

标签: android listview textview

我使用的自定义ListView包含ProgressBarTextView。现在,Progress Data可能包含float值,因此我想在TextView中显示浮点值。但它显示错误:无法解析'43 .20'到int。如果我通过int那么它工作正常。

如何防止这个问题?
我的示例代码:

    final SimpleAdapter adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic", "Accuracy", "Accuracy" },
new int[] { R.id.tvTopic, R.id.pbTopicAccuracy, R.id.tvAccuracy });

tvAccuracy是我正在谈论的TextView。

登录

03-11 12:32:38.913: E/AndroidRuntime(1187): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytestbuddy.anatomy/com.mytestbuddy.anatomy.ReportCard}: java.lang.NumberFormatException: unable to parse '43.24' as integer

4 个答案:

答案 0 :(得分:1)

首先要将浮动值转换为像这样的字符串

String Str = String.valueOf("43.50");

然后将str分配给textview。

TextView txt = new TextView(this);
txt.settext(Str);
像这样,试试这个。

答案 1 :(得分:1)

您可以连接浮点值

textView.setText(""+floatValue);

答案 2 :(得分:0)

在使用(int)进行类型转换之前使用Math.round()应该将float舍入到最接近的整数

答案 3 :(得分:0)

另一种方法也可以试试这个:

浮动值= 30.0F; Integer intValue = Integer.valueOf(value.intValue());

然后你可以将inValue转换为String格式,然后根据需要在Textview上设置String。

希望它能帮到你!!

相关问题