kotlin

Run Java to Kotlin converter from the command line?

自作多情 提交于 2020-07-18 10:19:04
问题 You can use the Kotlin plug-ins for Intellij and Eclipse to convert Java files to Kotlin. Can this conversion be done from the command line some how without either of these IDEs? 回答1: It seems that it's possible, but there's no tool for that, just code. There are tests in j2k module in Kotlin Github repository, the example below is taken from the tests (AbstractJavaToKotlinConverterSingleFileTest): private fun fileToKotlin(text: String, settings: ConverterSettings, project: Project): String {

Android and Kotlin coroutines: inappropriate blocking method call

与世无争的帅哥 提交于 2020-07-18 09:45:48
问题 I have the following class: class Repository( private val assetManager: AssetManager, private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO ) { suspend fun fetchHeritagesList(): HeritageResponse = withContext(ioDispatcher) { try { // TODO Blocking method call? val bufferReader = assetManager.open("heritages.json").bufferedReader() ... and I'm wondering why do I get a warning in the open("heritages.json") saying Innapropriate blocking method call ? isn't the withContext(ioDispatcher)

Kotlin Android- How implement CheckBox.OnCheckedChangeListener?

最后都变了- 提交于 2020-07-18 08:40:11
问题 I am new to Kotlin. I created a fragment and implemented View.OnClickListener and CheckBox.OnCheckedChangeListener . The View.OnClickListener works as expected but it shows Unresloved reference for CheckBox.OnCheckedChangeListener . The code is below class LoginFragment : Fragment(), View.OnClickListener, CheckBox.OnCheckedChangeListener { override fun onClick(view: View?) { } } How can I implement CheckBox.OnCheckedChangeListener ..? Thanks in advance 回答1: Use CheckBox

Does Kotlin have primitive types?

人走茶凉 提交于 2020-07-17 11:55:14
问题 Does Kotlin have primitive types?. When I declare the variable: val myAge: Int = 18 then the myAge variable stores the actual values is 18 or stores the addresses of the objects in the memory?. If Int is primitive type then why we can use its method like myAge.minus(10) ? 回答1: No... and yes. Kotlin doesn't have primitive type (I mean you cannot declare primitive directly). It uses classes like Int , Float as an object wrapper for primitives. When kotlin code is converted to jvm code, whenever

Safeargs library doesnt generate direction class

天涯浪子 提交于 2020-07-17 09:20:54
问题 I use navigation library and safeargs for passing data. I define argument to fragment like that. <fragment android:id="@+id/otherFragment" android:name="com.asd.navigate.OtherFragment" android:label="OtherFragment"> <argument android:name="screenTitle" android:defaultValue="0" app:type="string" /> </fragment> OtherFragmentArgs generated, I can use it but OtherFragmentDirection class doesnt generate when I click "make project". Is that bug or I have to do something different. Thnx for advice.

Kotlin: Coroutines scope vs Coroutine context

守給你的承諾、 提交于 2020-07-17 07:13:13
问题 Can anyone explain the difference between them? I think scope provides a reference(e.g. Job) to cancel them and context provides a reference to underlying thread. Is that so? 回答1: They are indeed closely related. You might say that CoroutineScope formalizes the way the CoroutineContext is inherited. CoroutineScope has no data on its own, it just holds a CoroutineContext . Its key role is as the implicit receiver of the block you pass to launch , async etc. See this example: runBlocking { val

Kotlin constructor (primary constructor)

耗尽温柔 提交于 2020-07-16 10:25:18
问题 I have a question about Kotlin constructor. class abc { constructor(a: Int) constructor(a: Int, e: Int) } class def(a: Int) { constructor(a: Int, e: Int) : this(a) } Why do I need to call this(a) in def class? What is different between class abc and def?? 回答1: The first class doesn't have a primary constructor while the second class has one. Per the documentation for Secondary Constructors you then have to delegate to it. If the class has a primary constructor, each secondary constructor

So I created a class to change layouts according to a construcotr but it wont work?

梦想与她 提交于 2020-07-16 08:25:23
问题 SO i have to classes CollegeSearch.kt and CollegeSwitcher.kt The first one make use of a recyclerview to put up a list for people to use and select a country. When they tap on it, I want the layout to switch by switching the class. here is the CollegeSearch.kt class: class CollegeSearch : AppCompatActivity() { var college_DU:MutableList<String> = ArrayList() var displayList:MutableList<String> = ArrayList() override fun onCreate(savedInstanceState: Bundle1?) { super.onCreate

How to solve Handler() deprecated?

三世轮回 提交于 2020-07-16 07:07:06
问题 Could anyone know, how to fix deprecated waring or any alternate solution for this. Handler().postDelayed({ context?.let { //code } }, 3000) 回答1: If you want to avoid the null check thing in Kotlin ( ? or !! ) you can use Looper.getMainLooper() if your Handler is working with some UI related thing, like this: Handler(Looper.getMainLooper()).postDelayed({ Toast.makeText(this@MainActivity, "LOOPER", Toast.LENGTH_SHORT).show() }, 3000) 回答2: Consider using coroutines scope.launch { delay(3000L) /

How to solve Handler() deprecated?

流过昼夜 提交于 2020-07-16 07:06:04
问题 Could anyone know, how to fix deprecated waring or any alternate solution for this. Handler().postDelayed({ context?.let { //code } }, 3000) 回答1: If you want to avoid the null check thing in Kotlin ( ? or !! ) you can use Looper.getMainLooper() if your Handler is working with some UI related thing, like this: Handler(Looper.getMainLooper()).postDelayed({ Toast.makeText(this@MainActivity, "LOOPER", Toast.LENGTH_SHORT).show() }, 3000) 回答2: Consider using coroutines scope.launch { delay(3000L) /