Kotlin-for-Android-Developers | Companion App for the book
kandi X-RAY | Kotlin-for-Android-Developers Summary
kandi X-RAY | Kotlin-for-Android-Developers Summary
In this book, I'll be creating an Android app from ground using Kotlin as the main language. The idea is to learn the language by example, instead of following a typical structure. I'll be stopping to explain the most interesting concepts and ideas about Kotlin, comparing it with Java 7. This way, you can see what the differences are and which parts of the language will help you speed up your work. This book is not meant to be a language reference, but a tool for Android developers to learn Kotlin and be able to continue with their own projects by themselves. I'll be solving many of the typical problems we have to face in our daily lives by making use of the language expressiveness and some other really interesting tools and libraries. The book is very practical, so it is recommended to follow the examples and the code in front of a computer and try everything it's suggested. You could, however, take a first read to get a broad idea and then dive into practice.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Kotlin-for-Android-Developers
Kotlin-for-Android-Developers Key Features
Kotlin-for-Android-Developers Examples and Code Snippets
Community Discussions
Trending Discussions on Kotlin-for-Android-Developers
QUESTION
I know Anko provides the functions parseSingle, parseOpt and parseList , I don't understand why the code of Android Developers (the book) need to design extensions parseList again.
Could you tell me? Thanks!
...ANSWER
Answered 2017-Nov-05 at 18:11Anko's parseList
takes a MapRowParser
, not a function. This simplifies usage. With Anko version you'd write
QUESTION
I'm learning the sample code about Anko at Kotlin for Android Developers (the book) https://github.com/antoniolg/Kotlin-for-Android-Developers
The Method 1 is from sample code and override parseList ,but it's hard to understand.
So I try to use the Method 2 instead of the Method 1, the Method 2 use original parseList function, but I get blank record when I use the Method 2, what error do I made in the Method 2
...ANSWER
Answered 2017-Nov-10 at 10:02I really do think you should stick to using the 'method 1' approach, it's a lot easier once you realise what Kotlin is letting you do. As I don't know how much you know about Kotlin, I'll try to cover this completely.
The existing class SelectQueryBuilder
has (I presume) a function called parseList
, that existing function takes a MapRowParser
. The MapRowParser
has a function parseRow
that takes a Map
and returns a T
.
In the old Java way of working, you would derive from MapRowParser
and would override parseRow
so that it does the conversion you want; converting the Map
into a DayForecast
(the generic T
would now have a type). An instance of this derived class is passed into the existing parseList
function. Your derived class would look something like
QUESTION
I get the error
java.lang.IllegalStateException: instance not initialized
you can test it at https://www.dropbox.com/s/gxeok1fyttsl54u/MyMirror.zip?dl=0
It seems that the code operator fun getValue(thisRef: Any?, property: KProperty<*>)...
in UIApp causes the error.
But the code is from a sample code, I hardly don't change, you can see source code at https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/ui/App.kt
and
Why?
...ANSWER
Answered 2017-Nov-13 at 20:56onCreate function in UIApp class is never called and thus an instance of UIApp is never initialized. This is happening because you didn't add UIApp class to AndroidManifest and because of that your app did not realize that you have your custom Application class in a project.
QUESTION
The following Code A is from https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/extensions/DelegatesExtensions.kt
I can use private var zipCode: Long by DelegatesExt.preference(this, ZIP_CODE, DEFAULT_ZIP)
to invoke when I use Code A.
I don't understand why the author wrap class Preference(...) with object DelegatesExt
I think Code B is more simple, I can use private val zipCode: Long by Preference(this, ZIP_CODE, DEFAULT_ZIP)
to invoke when I use Code B
Why need the class Preference be wrapped with object ?
Code A
...ANSWER
Answered 2017-Nov-13 at 06:53I can use private var zipCode: Long by DelegatesExt.preference(this, ZIP_CODE, DEFAULT_ZIP) to invoke when I use Code A.
I think Code B is more simple, I can use private val zipCode: Long by Preference(this, ZIP_CODE, DEFAULT_ZIP) to invoke when I use Code B
In the first case you can also import DelegatesExt.*
or DelegatesExt.preference
instead of DelegatesExt
and write by preference
.
Why need the class Preference be wrapped with object ?
It doesn't need to be (and I wouldn't do it), that's just the author's preference.
QUESTION
I'm learning the sample code about 《Kotlin for android developers》 at https://github.com/antoniolg/Kotlin-for-Android-Developers
In the code .parseList { DayForecast(HashMap(it)) }
, I can't understand what the function HashMap(it) will do. Is HashMap() a class and accept a parmater it
?
And more, I think that the full code of class DayForecast(...)..
is Code A, right?
Again, If I create a object var myDayForecast=DayForecast(10L,"Desciption",10,5,"http://www.a.com",10L)
, will myDayForecast.map be empty, right?
Code A
...ANSWER
Answered 2017-Nov-08 at 12:12I can't understand what the function
HashMap(it)
will do. IsHashMap()
a class and accept a parameterit
?
From the docs of HashMap(Map)
:
Constructs a new
HashMap
with the same mappings as the specifiedMap
. TheHashMap
is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specifiedMap
.
To answer your question: yes, HashMap
is a class, which accepts parameter it
which need to be an instance of Map
.
QUESTION
I'm learning the sample code about Anko at Kotlin for Android Developers (the book) at https://github.com/antoniolg/Kotlin-for-Android-Developers
I add a line var myMap=it
before DayForecast(HashMap(it))
, I think that the App will crash because the definition of parseList is parseList(parser: (Map) -> T)
, I can only assign a funtion to parseList.But the app can run correctly, why?
My modified ForecastDb.kt
...ANSWER
Answered 2017-Nov-07 at 16:08The signature of the function is parseList(parser: (Map) -> T)
. The (Map) -> T)
as a parameter means that the parseList
function takes as an argument a function type with a signature of a single argument (Map
) that returns a T
.
This is an example of higher-order functions).
In your example, you are passing in a lambda expression:
QUESTION
The following sample code is from https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/data/db/ForecastDb.kt
I hope to display the value of "it" in the code .parseList{ DayForecast(HashMap(it)) }
So I set a breakpoint before the code val dailyForecast ...
, and click Run -> Debug 'app menu in Android Studio, the press F7 to start Step Into, but I can't find how I can the value of "it" in the code .parseList{ DayForecast(HashMap(it)) }
How can I do? Thanks!
...ANSWER
Answered 2017-Nov-07 at 06:45You can give it a name temporarily:
QUESTION
The following sample code is from Kotlin-for-Android-Developers at https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/data/db/ForecastDb.kt
I can't understand completely the code DayForecast(HashMap(it))
. what does "it" mean?
And more, what happend when the parseList { DayForecast(HashMap(it)) }
is executed ?
ANSWER
Answered 2017-Nov-02 at 02:46it is the implicit name of a single parameter. Check doc here.
QUESTION
The Code A is from https://github.com/antoniolg/Kotlin-for-Android-Developers/blob/master/app/src/main/java/com/antonioleiva/weatherapp/data/db/ForecastDb.kt
I hope to use return instead of let, so I write the Code B, but the Code B can't be compiled, and I get two errors, why?
Error:(30, 10) 'return' is not allowed here
Error:(30, 45) Type mismatch: inferred type is CityForecast? but CityForecast was expected
The Code A
...ANSWER
Answered 2017-Nov-05 at 08:24convertToDomain
is expecting an non-null CityForecast
instead of nullable CityForecast?
. So, using let
is a better approach. Otherwise, you have to force unwrap the nullable city
like this:
QUESTION
I'm a beginner of Kotlin. The following code is from Kotlin-for-Android-Developers at
The fun startActivity have two parameter DetailActivity.ID to it.id
and DetailActivity.CITY_NAME to result.city
.
Could you explain the details about the two parameters?
...ANSWER
Answered 2017-Jul-17 at 08:40to
is an infix function that creates a pair from to instances:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Kotlin-for-Android-Developers
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page