kotlin

Why GSON fails to convert Object when it's a field of another Object? [duplicate]

故事扮演 提交于 2021-01-29 15:34:48
问题 This question already has answers here : Gson doesn't serialize fields defined in subclasses (4 answers) Closed yesterday . GSON fails to convert Errorneous to JSON properly when it's inside of other Object . But it works well when it's converted as a top level object. Why, and how to fix it? Example: import com.google.gson.GsonBuilder sealed class Errorneous<R> {} data class Success<R>(val result: R) : Errorneous<R>() data class Fail<R>(val error: String) : Errorneous<R>() class Container

check value exists or not using EncryptedSharedPreferences android

旧街凉风 提交于 2021-01-29 15:26:20
问题 I use the EncryptedSharedPreferences to store value while login...i want to change text in navigation drawer of home activity so when user is loggedin it will show logout else it will show login navigation drawer Loginactivity:-------- sharedPreferences = EncryptedSharedPreferences.create( "secret_shared_prefs", masterKeyAlias, baseContext, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ) as

Is there a more elegant way to check if a String is a valid Int in Kotlin?

笑着哭i 提交于 2021-01-29 15:24:51
问题 This is Kotlin/JVM I currently have this code : fun String.isValidInt(): Boolean { var isInt: Boolean = false try { if (this.toInt().toString() == this) { isInt = true } } catch (e: NumberFormatException) { isInt = false } return isInt } and I was wondering if there was a more elegant way to approach this, specficially the way I set the return value isInt to true or false. Here is a playground link to my code and a main function to test with. 回答1: For a start, try can return an expression.

How to use Single Live Event to show toast in Kotlin

巧了我就是萌 提交于 2021-01-29 15:22:43
问题 I want to use single live event class to show toast (like flag) Here is my code what I tried. I want to not use peding like flag. how do I fix it? MainViewModel class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() { val keyword = MutableLiveData<String>() val movieList = MutableLiveData<List<Movie>>() val msg = MutableLiveData<String>() val pending: AtomicBoolean = AtomicBoolean(false) fun findMovie() { val keywordValue = keyword.value ?: return pending.set(true) if

How to return failed task result in continuation task [duplicate]

霸气de小男生 提交于 2021-01-29 15:06:27
问题 This question already has an answer here : How to return failed task result in continuation task? (1 answer) Closed 9 days ago . I'm writing my first app in Kotlin and am using Firestore & Firebase Storage. In the process of deleting a document, I want to delete all files in Storage that the document references (as it is the only reference to them in my case). If the Storage delete fails, I want to abort the document delete, in order to avoid orphan files in my Storage. I also want to do

How to find a string within another, ignoring some characters?

久未见 提交于 2021-01-29 15:00:22
问题 Background Suppose you wish to find a partial text from a formatted phone number, and you wish to mark the finding. For example, if you have this phone number: "+972 50-123-4567" , and you search for 2501 , you will be able to mark the text within it, of "2 50-1". More examples of a hashmap of queries and the expected result, if the text to search in is "+972 50-123-45678", and the allowed characters are "01234567890+*#" : val tests = hashMapOf( "" to Pair(0, 0), "9" to Pair(1, 2), "97" to

After a CustomPrettyPrinter in jackson, Indentation settings are not working

陌路散爱 提交于 2021-01-29 13:50:43
问题 class CustomPrettyPrinter: DefaultPrettyPrinter() { @Throws(IOException::class) override fun writeObjectFieldValueSeparator(jg: JsonGenerator) { jg.writeRaw(": ") } override fun createInstance(): DefaultPrettyPrinter? { return CustomPrettyPrinter() } } fun generateJson() { val file = File("file.json") var jsonData = JSONParser().parse(FileReader(file)) as JSONObject val mapper = ObjectMapper() val indenter : DefaultPrettyPrinter.Indenter = DefaultIndenter(" ", DefaultIndenter.SYS_LF) val

Error: Declaration annotated with @OptionalExpectation can only be used in common module sources

我与影子孤独终老i 提交于 2021-01-29 13:24:52
问题 Update (2018-11-03) This is a known issue that is being tracked here. Original Post I am playing around with Kotlin MPP, specifically with Kotlin 1.3 and the new structure. After converting a Kotlin 1.2 MPP to the new structure, I keep getting these errors: Error:(3, 18) Kotlin: [MPP_jvmMain] Declaration annotated with '@OptionalExpectation' can only be used in common module sources These are referring to the @JsName() annotations I have within my common module. I have tried: Starting a

Notifications are not working with kotlin

試著忘記壹切 提交于 2021-01-29 13:14:34
问题 I'm trying to send a local notification when a user reaches a specific phase in the android application built with kotlin. Here is the code I'm using : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply { setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1) } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {

How do I parse through JSON Arrays in Kotlin

旧巷老猫 提交于 2021-01-29 12:44:22
问题 so I am working on an Android app and I need to get a list of users from my mysql database and put their data into a User class in my kotlin app. The php api is working correctly and I get this response with Postman in JSON: { "id": "5f69bbc1b264c6.07041576", "username": "Test F", "email": "test@gmail.com", "profileimagepath": "../uploads/profileimages/5f69bbc1b264c6.07041576.jpg" }{ "id": "5f69c5708632f7.37478122", "username": "Test H", "email": "test@gmail.com", "profileimagepath": "..