Kotlin for Android Developers
sort Returns a sorted list of all elements. 1 assertEquals(listOf(2, 3, 5, 7), unsortedList.sort()) sortBy
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 19 Saving and requesting data from database
- 19.1 Creating database model classes
sort
Returns a sorted list of all elements. 1 assertEquals(listOf(2, 3, 5, 7), unsortedList.sort()) sortBy Returns a list of all elements, sorted by the specified comparator. 1 assertEquals(listOf(3, 7, 2, 5), unsortedList.sortBy { it % 3 }) sortDescending Returns a sorted list of all elements, in descending order. 18 Collections and functional operations 80 1 assertEquals(listOf(7, 5, 3, 2), unsortedList.sortDescending()) sortDescendingBy Returns a sorted list of all elements, in descending order by the results of the specified order function. 1 assertEquals(listOf(2, 5, 7, 3), unsortedList.sortDescendingBy { it % 3 }) 19 Saving and requesting data from database A previous chapter covered the creation of a SQLiteOpenHelper , but now we need a way to use it to persist our data into the database and recover it when necessary. Another class, called ForecastDb , will make use of it. 19.1 Creating database model classes But first, we are going to create the model classes for the database. Do you remember the map delegates we saw? We are using them to map those fields directly to the database and viceversa. Let’s take a look at the CityForecast class first: 1 class CityForecast(val map: MutableMap 2 val dailyForecast: List 3 var _id: Long by map 4 var city: String by map 5 var country: String by map 6 7 constructor(id: Long, city: String, country: String, 8 dailyForecast: List 9 : this(HashMap(), dailyForecast) { 10 this._id = id 11 this.city = city 12 this.country = country 13 } 14 } The default constructor is getting a map, presumably filled with the values of the properties, and a dailyForecast . Thanks to the delegates, the values will be mapped to the corresponding properties based on the name of the key. If we want to make the mapping work perfectly, the names of the properties must be the same as the names of the columns in the database. We’ll see why later. But then, a second constructor is necessary. This is because we’ll be mapping classes from the domain to classes for the database, so instead of using a map, extracting the values from the properties will be more convenient. We pass an empty map, but again, thanks to the delegate, when we set a value 81 19 Saving and requesting data from database 82 to a property, it automatically adds a new value to the map. That way, we’ll have our map ready to be added to the database. After some useful code, you will see it works like magic. Now we need a second class, DayForecast , which corresponds to the second table. This one will basically have one property per column, and will also use a secondary constructor. The only difference is that we are not assigning an id, because it will be auto-generated by SQLite. 1 class DayForecast(var map: MutableMap 2 var _id: Long by map 3 var date: Long by map 4 var description: String by map 5 var high: Int by map 6 var low: Int by map 7 var iconUrl: String by map 8 var cityId: Long by map 9 10 constructor(date: Long, description: String, high: Int, low: Int, 11 iconUrl: String, cityId: Long) : this(HashMap()) { 12 this.date = date 13 this.description = description 14 this.high = high 15 this.low = low 16 this.iconUrl = iconUrl 17 this.cityId = cityId 18 } 19 } These classes will help us map the data between objects and SQLite tables, in both directions. Download 1.04 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling