0

How do I call setText() using float data type? I have already tried this

if (response.isSuccessful) {
      val destination = response.body()
      destination?.let {
              client_names.setText(destination.client_name)
              product_names.setText(destination.product_name)

              amounts.setText(String.valueOf(destination.amount)) ----->
              prices.setText(String.valueOf(destination.price))   ----->

              comments.setText(destination.comment)

              collapsing_toolbar.title = destination.client_name + destination.product_name
         }

enter image description here

I have tried this answer from this link

How to call setText() using a Float data type?

but I get this error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.smartherd.globofly, PID: 8999
    java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
        at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
        at java.lang.Float.parseFloat(Float.java:451)
        at com.smartherd.globofly.activities.DestinationDetailActivity$loadDetails$1.onResponse(DestinationDetailActivity.kt:58)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 8999 SIG: 9
jamesc
  • 12,423
  • 15
  • 74
  • 113
  • Doesn't a `Float` have a `toString()` method in Kotlin? I think it does... Try that if you haven't already. – deHaar Jul 13 '20 at 12:20
  • 1
    `destination.amount.toString()` – p2pdops Jul 13 '20 at 12:20
  • Have you tried this. https://stackoverflow.com/a/8907890/6264949 This might help you. – Tushar Deshpande Jul 13 '20 at 12:20
  • @TusharDeshpande yes, ive already tried that –  Jul 13 '20 at 12:21
  • @p2pdops just like this ? amounts.setText(String.valueOf(destination.amount.toString())) –  Jul 13 '20 at 12:21
  • actually it is better to follow @webfreak answer because you may want to add currency before amount so you can `amounts.setText("Curr: ${destination.amount}")` that would display `Curr: 100` (if destination.amount is 100) – p2pdops Jul 13 '20 at 12:36

1 Answers1

1

If you don't need any formatting just do:

setText("${destination.amount}")
Simon
  • 1,657
  • 11
  • 16