Jeeten Parmar
Jeeten Parmar

Reputation: 5787

Display float value in TextView using SimpleAdapter

I am using Custom ListView which contains ProgressBar and TextView. Now, Progress Data may contain float value so I want to display float value in TextView. But It is showing error : can not parse '43.20' to int. If I am passing int then It is working fine.

How to prevent this problem ?
My Sample Code :

    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 is the TextView about which I am talking.

Log:

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

Upvotes: 1

Views: 2070

Answers (4)

Tarun - Systematix
Tarun - Systematix

Reputation: 104

Another way you can try this one also:

Float value = 30.0F; Integer intValue=Integer.valueOf(value.intValue());

and then you can convert inValue to String format and then set String on Textview as you need.

Hope it will help you!!

Upvotes: 0

John
John

Reputation: 8946

You can concat the float value

textView.setText(""+floatValue);

Upvotes: 1

saravanan
saravanan

Reputation: 388

First u nee to convert floating values to string like this

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

Then assign str to textview.

TextView txt = new TextView(this);
txt.settext(Str);

like this,Try this one.

Upvotes: 1

Lokesh
Lokesh

Reputation: 5378

Use Math.round() before typecasting using (int) should round the float to the nearest whole number

Upvotes: 0

Related Questions