Why am I not getting the correct result when I use the original parseList function in Kotlin?

I am studying an example code about Anko in Kotlin for Android developers (book) https://github.com/antoniolg/Kotlin-for-Android-Developers

Method 1 from the sample code and overrides parseList, but it is difficult to understand.

So, I'm trying to use method 2 instead of method 1, method 2 uses the original parseList function, but I get an empty entry when I use method 2, what error did I make in method 2

class DayForecast(var map: MutableMap<String, Any?>) {
  var _id: Long by map
  var date: Long by map
  var description: String by map
  var high: Int by map
  var low: Int by map
  var iconUrl: String by map
  var cityId: Long by map

  constructor(date: Long, description: String, high: Int, low: Int,
              iconUrl: String, cityId: Long) : this(HashMap()) {
    this.date = date
    this.description = description
    this.high = high
    this.low = low
    this.iconUrl = iconUrl
    this.cityId = cityId
  }
}

Method 1

override fun requestForecastByZipCode(zipCode: Long, date: Long) =
  forecastDbHelper.use {
    val dailyRequest = "${DayForecastTable.CITY_ID} = ? AND ${DayForecastTable.DATE} >= ?"
    val dailyForecast = select(DayForecastTable.NAME)
          .whereSimple(dailyRequest, zipCode.toString(), date.toString())
          .parseList { DayForecast(HashMap(it)) }
    /* common code block */
  }

fun <T : Any> SelectQueryBuilder.parseList(parser: (Map<String, Any?>) -> T):
        List<T> = parseList(object : MapRowParser<T> {
  override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
})

Method 2

override fun requestForecastByZipCode(zipCode: Long, date: Long) = 
  forecastDbHelper.use {
    val dailyRequest = "${DayForecastTable.CITY_ID} = ? AND ${DayForecastTable.DATE} >= ?"
    val dailyForecast = select(DayForecastTable.NAME)
          .whereSimple(dailyRequest, zipCode.toString(), date.toString())
          .exec { parseList(classParser<DayForecast>()) }
    /* common code block */
  }
+4
source share
1 answer

, " 1", , , . , , .

SelectQueryBuilder ( ) , parseList, MapRowParser<T>. MapRowParser<T> parseRow, Map<String, Any?> a T.

Java- MapRowParser<T> parseRow , ; Map<String, Any?> DayForecast ( T ). parseList. :

class MapToDayForecastRowParser extends MapRowParser<DayForecast> {
  @Override public DayForecast parseRow(Map<String, Object> map) {
    // Note that Java "Object" is more or less Kotlin "Any?"
    return new DayForecast(map); // Might need to convert the map type btw
  }
}

// . , .. parseList , Map<String, Any?> T ( , DayForecast(HashMap(it)), it , Map. parseList, , . , - , Kotlin .

, , - , .

// Java
new MapRowParser<T>() {
  @Override public T parseRow(Map<String, Object>) {
     /* Map to T logic */
  }
}
// Kotlin
object : MapRowParser<T> {
 override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
}

Kotlin "". parser, parseRow. , , , , .

Kotlin , , . , , parseList{ it.getOrDefault("name", "unkown_user") }. " , , ?".

+2

Source: https://habr.com/ru/post/1688670/


All Articles