kotlin

How to use Kotlin when to find if a string is numeric?

安稳与你 提交于 2021-01-21 12:11:07
问题 I'd like to use a when() expression in Kotlin to return different values from a function. The input is a String , but it might be parsable to an Int , so I'd like to return the parsed Int if possible, or a String if it is not. Since the input is a String I can not use the is type check expression. Is there any idiomatic way to achieve that? Edit: My problem is how the when() expression should look like, not about the return type. 回答1: Version 1 (using toIntOrNull and when when as requested)

Kotlin sorting nulls last

谁说胖子不能爱 提交于 2021-01-21 12:04:07
问题 What would be a Kotlin way of sorting list of objects by nullable field with nulls last? Kotlin object to sort: @JsonInclude(NON_NULL) data class SomeObject( val nullableField: String? ) Analogue to below Java code: @Test public void name() { List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa")) .sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder()))) .collect(toList()); assertEquals("aaa",

Kotlin sorting nulls last

我们两清 提交于 2021-01-21 12:03:13
问题 What would be a Kotlin way of sorting list of objects by nullable field with nulls last? Kotlin object to sort: @JsonInclude(NON_NULL) data class SomeObject( val nullableField: String? ) Analogue to below Java code: @Test public void name() { List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa")) .sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder()))) .collect(toList()); assertEquals("aaa",

How to set click listener and pass edittext fields value to view model using data binding

喜夏-厌秋 提交于 2021-01-21 07:24:11
问题 Hi I am trying to use data binding and mvvm architecture in my android app. I want to add click listener using data binding in the layout and send the values of username and password edittext to the view model and it will execute the web service and call appropriate method of LoginActivity like startHomeActivity() . Does anyone know how to do this or Am I taking wrong approach ? I have below snippet of code of my activity, layout and view model LoginActivity.kt class LoginActivity :

Upload a file to Firebase Storage and get a downloadUrl. How can I return the result in a Kotlin function?

大憨熊 提交于 2021-01-21 05:38:45
问题 I have a function that uploads a local file to Firestore Storage using uploadTask . I followed the instructions given in the docs. Here is my code: fun uploadAudioFile(file: File){ val audioFilePathUri = Uri.fromFile(file) val ref = currentUserRef.child("audioFiles/" + System.currentTimeMillis() + "." + "m4a") val uploadTask = ref.putFile(audioFilePathUri) val urlTask = uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task -> if (!task.isSuccessful) { task

Kotlin Coroutines with timeout

拟墨画扇 提交于 2021-01-21 05:09:28
问题 I'm currently writing a test-function which should run a block or (when a certain timeout is reached) throws an exception. I was trying this with Coroutines in Kotlin but ended up with a mixture of Coroutines and CompletableFuture : fun <T> runBlockWithTimeout(maxTimeout: Long, block: () -> T ): T { val future = CompletableFuture<T>() // runs the coroutine launch { block() } return future.get(maxTimeout, TimeUnit.MILLISECONDS) } This works, but I'm not sure if this is the intended way to

How to use android navigation without binding to UI in ViewModel (MVVM)?

≯℡__Kan透↙ 提交于 2021-01-20 19:31:57
问题 I am using android navigation that was presented at Google I/O 2018 and it seems like I can use it by binding to some view or by using NavHost to get it from Fragment. But what I need is to navigate to another specific view from ViewModel from my first fragment depending on several conditions. For ViewModel , I extend AndroidViewModel , but I cannot understand how to do next. I cannot cast getApplication to Fragment/Activity and I can't use NavHostFragment . Also I cannot just bind navigation

What does Void return type mean in Kotlin

怎甘沉沦 提交于 2021-01-20 17:44:48
问题 I tried to create function without returning value in Kotlin. And I wrote a function like in Java but with Kotlin syntax fun hello(name: String): Void { println("Hello $name"); } And I've got an error Error:A 'return' expression required in a function with a block body ('{...}') After couple of changes I've got working function with nullable Void as return type. But it is not exactly what I need fun hello(name: String): Void? { println("Hello $name"); return null } According to Kotlin

Migrate to the new kotlin-parcelize

谁都会走 提交于 2021-01-20 16:31:29
问题 Can anyone please explain how to migrate to the new kotlin-parcelize ? I tried: Replace apply plugin: 'kotlin-android-extensions' with apply plugin: 'kotlin-parcelize' in app build.gradle Replace import kotlinx.android.parcel.Parcelize with import kotlinx.parcelize.Parcelize The latter one leads to Class 'ParcelZonedDateTime' is not abstract and does not implement abstract member public abstract fun describeContents(): Int defined in android.os.Parcelable on e.g. this code: import androidx

Migrate to the new kotlin-parcelize

一个人想着一个人 提交于 2021-01-20 16:25:04
问题 Can anyone please explain how to migrate to the new kotlin-parcelize ? I tried: Replace apply plugin: 'kotlin-android-extensions' with apply plugin: 'kotlin-parcelize' in app build.gradle Replace import kotlinx.android.parcel.Parcelize with import kotlinx.parcelize.Parcelize The latter one leads to Class 'ParcelZonedDateTime' is not abstract and does not implement abstract member public abstract fun describeContents(): Int defined in android.os.Parcelable on e.g. this code: import androidx