Closed Diy2210 closed 4 years ago
If I understand correctly, what are you trying to do, you can use string template feature as shown below
text = data.map {
if (it.total.isNotEmpty()) {
"${bytesToGB(it.total.toLong())} Gb".desc()
} else {
it.total.desc()
}
}
If I understand correctly, what are you trying to do, you can use string template feature as shown below
text = data.map { if (it.total.isNotEmpty()) { "${bytesToGB(it.total.toLong())} Gb".desc() } else { it.total.desc() } }
I try it, but dont work, error returns!
Can you provide more information about what kind of error is it?
@Diy2210 Try to use this construction:
text = data.map {
if (it.total.isNotEmpty()) {
"${bytesToGB(it.total.toLong())} Gb".desc()
} else {
it.total.desc()
} as StringDesc
}
Try to use this construction:
Oh, dont work too
Print error:
Error:
My Mutable Live Data:
Save Value to Data:
Looks like problem is that it.total
is not a number. Something like this should resolve problem.
text = data.map {
val longTotal = it.total.toLongOrNull()
if (longTotal != null) {
"${bytesToGb(longTotal)} Gb".desc()
} else {
it.total.desc()
} as StringDesc
}
Something like this should resolve problem.
Yeah, its work! Tnx bro!
And one more question, I try get percents *_(free 100) / total_, but an error is returned**.
longFree!!
will cause crash if it is null
, you should move it inside if statement
if (longFree != null && longTotal != null) {
val freePerCents = (longFree * 100) / longTotal
"${freePerCents} %".desc()
}
You can read more about null-safety in Kotlin documentation https://kotlinlang.org/docs/reference/null-safety.html
I get it, all works goog, tnx again!
Hi, I try convert value to byte size to Gb and set to TextWidget in UnitItem. But in block text = data.map { ... } I get an error. And I don't understand how add in this block value + "String" like text = data.map { it.total.desc() + "Gb" as StringDesc() } Always return Error. In Android I use android.text.format.Formatter.formatShortFileSize and all works.
Value("total" in Kb):
My List:
My fun TableUnits:
Class unit items where I create TextWidget:
Convert fun to Mb/Gb: