(immagine creative commonds)
COS'E' KOTLIN?
DICHIARAZIONE DI UNA VARIABILE
-val quando la variabile è statica e quindi non si può modificare.
-var invece quando è dinamica e può essere sempre modificata.
DEDUZIONE DI TIPO
In questo caso Kotlin capirà che quella variabile sarà di tipo stringa.
All'interno di kotlin esistono 2 tipi di liste, che sono praticamente degli array ed hanno il compito di contenere dati, anche in base alle esigenze del programmatore.
Il modo classico di rappresentarle è la MutableList:
val nomeLista : MutableList<String>=mutableListOf()
nomelista.add("ciao")
In questo caso abbiamo creato una lista chiamata nomeLista, che può contenere solo dati di tipo stringa e abbiamo aggiunto a quella lista la parola ciao.
VALORE NULL
Le variabili in Kotlin non possono contenere valori null in modo predefinito, ma possiamo aggiungere noi l'opzione di contenere valori null aggiungendo il «?» dopo il tipo...ovviamente come ci sarete già arrivati, se inseriamo il punto interrogativo, non avrà più effetto la deduzione.
CONDIZIONI INLINE
Queste funzioni hanno lo stesso comportamento delle condizioni classiche dei normali linguaggi di programmazione, ma permette anche di inserire codice condizionali a destra di un’assegnazione, Ad esempio Una cascata di if-else è sostituibile, inoltre, con un when.
In kotlin con la parola **fun **si possono dichiarare delle funzioni come anche in java o c. Il tipo di ritorno di quella funzione si esprime dopo il passaggio dei parametri, seguito dal simbolo «:»
PASSAGGIO FRA ACTIVITY
In kotlin si può effettuare un passaggio fra una Activity ed un’altra, ma isogna prima effettuare delle modifiche...bisogna innanzitutto dichiarare un Intent, passando come parametri l’activity di partenza e l’activity di destinazione, per poi utilizzare il comando startActivity:
val myIntent = Intent(this, OtherActivity::class.java)startActivity(myIntent)
DIALOGFRAGMENT
Una ListView permette di visualizzare delle stringhe in un elenco.
Per crearle si utilizza il componente ListView insieme ad un oggetto layout di tipo TextView.
Creato il layout , si può utilizare anche un adattatore, che ha lo scopo di rendere a tutto lo schermo la nostra schermata, anche avendo dimensioni diverse... si utilizza il comando ArrayAdapte) che associ a ogni elemento della lista la riga «single_row».
ELEMENTI
(creative commons image)
WHAT IS KOTLIN?
DECLARATION OF A VARIABLE
-val when the variable is static and therefore cannot be modified.
-var instead when it is dynamic and can always be modified.
TYPE DEDUCTION
In this case Kotlin will understand that that variable will be of type string.
Within kotlin there are 2 types of lists, which are practically arrays and have the task of containing data, also according to the needs of the programmer.
The classic way to represent them is the MutableList:
val listName : MutableList<String>=mutableListOf()
listname.add("hello")
In this case we have created a list called listName, which can only contain data of type string, and we have added the word hello to that list.
NULL VALUE
Variables in Kotlin cannot contain null values by default, but we can add the option to contain null values ourselves by adding the «?» after the type... obviously as you will have already arrived there, if we insert the question mark, the deduction will no longer have effect.
INLINE TERMS
These functions have the same behavior as the classic conditions of normal programming languages, but it also allows you to insert conditional code to the right of an assignment. For example, an if-else cascade can also be replaced with a when.
In kotlin with the word **fun ** you can declare functions as well as in java or c. The return type of that function is expressed after passing the parameters, followed by the symbol «:»
SWITCH BETWEEN ACTIVITIES
In kotlin you can switch between one Activity and another, but you must first make some changes... first of all you need to declare an Intent, passing the starting activity and the destination activity as parameters, and then using the command startActivity:
val myIntent = Intent(this, OtherActivity::class.java)startActivity(myIntent)
DIALOGFRAGMENT
A ListView allows you to view strings in a list.
To create them you use the ListView component together with a layout object of type TextView.
Once the layout has been created, an adapter can also be used, which has the purpose of making our screen the whole screen, even if it has different dimensions ... the ArrayAdapte command is used which associates the «single_row» line to each element of the list .
ELEMENTS