我们使用的是不可修改的对象,当我们需要修改这个对象状态,我们可以拷贝一个对象只修改需要修改的属性
val f1 = Forecast(Date(), 27.5f, "Shiny day")
val f2 = f1.copy(temperature = 30f)
映射对象的每一个属性到一个变量中,这个过程就是多声明,例如:
val f1 = Forecast(Date(), 27.5f, "Shiny day")
val (date, temperature, details) = f1
这个多声明编译之后就是这样的:
val date = f1.component1()
val temperature = f1.component2()
val details = f1.component3()
接下来,我们来获取天气数据,组成一个列表
先来获取数据,有数据,就需要数据类来承载数据,定义数据类:
data class Temperature(val day: Float, val min: Float, val max: Float, val night: Float, val eve: Float, val morn: Float)
data class ForecastResult(val city: City, val list: List)
data class Forecast(val dt: Long, val temp: Temperature, val pressure: Float, val humidity: Int, val weather: List, val speed: Float, val deg: Int, val clouds: Int, val rain: Float)
data class Coordinates(val lon: Float, val lat: Float)
data class City(val id: Long, val name: String, val coord: Coordinates, val country: String, val population: Int)
data class Weather(val id: Long, val main: String, val description: String, val icon: String)
然后获取json数据,转换json到数据类
public class ForecastRequest(val zipCode: String) {
//companion object中可以一些静态的属性、常量或者函数,这个对象被这个类所有对象共享
companion object {
private val APP_ID = "15646a06818f61f7b8d7823ca833e1ce"
private val URL = "http://api.openweathermap.org/data/2.5/" + "forecast/daily?mode=json&units=metric&cnt=7"
private val COMPLETE_URL = "$URL&APPID=$APP_ID&q="
}
fun execute(): ForecastResult {
val forecastJsonStr = URL(COMPLETE_URL + zipCode).readText()
return Gson().fromJson(forecastJsonStr, ForecastResult::class.java)
}
}
在类中记得导入import java.net.URL,然后gradle中引入anko
在这里,我们需要转换一下数据
//如果有两个相同名字的类,可以给起个别名
import com.kavenka.testkotlin.domain.Forecast as ModelForecast
public class ForecastDataMapper {
fun convertFromDataModel(forecast: ForecastResult): ForecastList {
return ForecastList(forecast.city.name, forecast.city.country, convertForecastListToDomain(forecast.list))
}
private fun convertForecastListToDomain(list: List): List {
//list.map可以循环这个集合并且返回一个转换后的新的list
return list.map { convertForecastItemToDomain(it) }
}
private fun convertForecastItemToDomain(forecast: Forecast): ModelForecast {
return ModelForecast(convertDate(forecast.dt), forecast.weather[0].description, forecast.temp.max.toInt(), forecast.temp.min.toInt())
}
private fun convertDate(date: Long): String {
val df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault())
return df.format(date * 1000)
}
}
创建一个执行任务的接口类,在创建一个实现这个接口的请求数据类:
public interface Command {
fun execute(): T
}
class RequestForecastCommand(val zipCode: String) : Command {
override fun execute(): ForecastList {
val forecastRequest = ForecastRequest(zipCode)
return ForecastDataMapper().convertFromDataModel(
forecastRequest.execute())
}
}
上面的步骤其实就是1.定义数据类,2.包装数据类,3.定义请求数据接口,4.获取数据转化为数据实体类
最后,将数据和recycleView绑定
class ForecastListAdapter(val weekForecast: ForecastList) : RecyclerView.Adapter() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(TextView(parent.getContext()))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
with(weekForecast.dailyForecast[position]) {
holder.textView.text = "$date - $description - $high/$low"
}
}
override fun getItemCount(): Int = weekForecast.dailyForecast.size
class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
}
使用anko获取数据,后续将重点学习介绍一下anko
doAsync() {
val result = RequestForecastCommand("94043").execute()
uiThread {
forecastList.adapter = ForecastListAdapter(result)
}
}
最后的运行效果: