kotlin

How to instantiate ViewModel that extends AndroidViewModel?

此生再无相见时 提交于 2021-01-29 12:28:56
问题 I'm following a tutorial where a ViewModel extends an abstract class in order to use coroutines, this is the class that extends: abstract class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.Main override fun onCleared() { super.onCleared() job.cancel() }} And this is the ViewModel: class ViewModel(application: Application) : BaseViewModel(application) {

MongoDB filter nested array

北城以北 提交于 2021-01-29 12:20:55
问题 I have a collection that looks something like: [ { "_id": 1, "title": "dummy title", "settings": [ { "type": "light", "status": "enabled" }, { "type": "flare", "status": "disabled" }, { "type": "toolbar", "status": "enabled" } ] } ] I wanna fetch it by Id but only with the "enabled" settings and not sure how the aggregation pipeline should look like. I supposed to create the query using mongoTemplate in Java / Kotlin but even just the mongo query would be enough. 回答1: You can do it with a

How to get GSON & Retrofit to serialize and map an array from a JSON response into a String?

半腔热情 提交于 2021-01-29 12:18:19
问题 I am making an API request which returns some array values. I need to serialize these array values so that I can assign them to their corresponding class attributes (which are String types). Now I know how to use GSON to serialize and deserialize lists, but with Retrofit the mapping is done automatically. This means that if my attribute is of type String, the API call returns the error "Expected a String but received an Array instead". How do I get around this so that I can receive them as

Build error due to conflict between Androidx and Android Support recent version

陌路散爱 提交于 2021-01-29 11:34:56
问题 I am stuck with a build error due to some conflict as defined in the title. Here is my error: Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory). Here are my build.grade files: apply plugin: 'com.android.application' apply plugin

Decimal to vulgar fraction conversion method- need help converting from Swift to Kotlin

跟風遠走 提交于 2021-01-29 11:06:23
问题 I found this beautiful decimal to fraction function: https://gist.github.com/natecook1000/9ecc976aaac9a035bddf I have manipulated the above for my apps necessity and would like help in making a Kotlin version. I have indeed tried making the conversion myself but I am a newbie coder period and have only been at Kotlin 3 weeks now. I don't have enough experience matching syntax and semantics between languages to do it all myself. Help appreciated :) I changed the above to output custom Unicode

retrofit retry with the new token request?

雨燕双飞 提交于 2021-01-29 10:53:24
问题 I get new token when any request return TOKEN_IS_NOT_ACTIVE then I get a new token In interceptor override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() var response = chain.proceed(request) val bodyString = response.peekBody(AppConstants.BUFFER_SIZE_2MB)?.string() val json = JSONObject(bodyString) //after get new token retry with the failedRequest if(json.has("token"){ failedRequest=failedRequest.newBuilder().removeHeader("Authorization").removeHeader("")

Set null to Thread to prevent memory leak in onDestroy Android

僤鯓⒐⒋嵵緔 提交于 2021-01-29 10:50:20
问题 I am using Thread for some Async operations. Now primarily I have 3 Threads like : private lateinit var horse1Thread: Thread private lateinit var horse2Thread: Thread private lateinit var timerThread: Thread Calling stop() in onDestroy() of activity causes a UnsupportedOperationException and I thought of setting these values to null to allow GC collection and prevent memory leak. Since my fields are non-null types I cant set them to null . So is this my only option? Or does kotlin provide a

list is not getting in recyclerview using retrofit

Deadly 提交于 2021-01-29 10:48:33
问题 basically im trying display list in recyclerview using retrofit and viewmodel........ on debugging the onresponse im getting 200 response but why is it not displaying list in recyclerview i dont know where im going wrong i will post up more codes if needed following is the code:--- class Tables : BaseClassActivity() { lateinit var recyclerView: RecyclerView lateinit var recyclerAdapter: TableAdapter var Tablelist : MutableList<Tabledata> = mutableListOf() override fun onCreate

Android Binding Adapter attribute not found

一个人想着一个人 提交于 2021-01-29 10:11:50
问题 I am using a binding adapter to have mutable text in one of my views. I believe I have it implemented correctly (and it's working in other places), but for the case of mutableText, it's getting the error AAPT: error: attribute mutableText not found I've looked through some other answers on here, but none of them have been able to solve the issue. Here is my layout file: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http:

Set cache expireAfterWrite property dynamically - Caffeine and Spring WebFlux

守給你的承諾、 提交于 2021-01-29 09:41:11
问题 I am using caffeine cache to store an authorisation token that has been obtained using webClient WebFlux. I have set the expireAfterWrite to a hardcoded value in the application.yml file as follows: spring: cache: cache-names: accessTokens caffeine: spec: expireAfterWrite=100m The token is obtained using a WebClient with Spring WebFlux as below code depicts: @Autowired var cacheManager: CacheManager? = null override fun getAuthToken(): Mono<AccessToken> { val map = LinkedMultiValueMap<String,