4.0 数据类

我们使用的是不可修改的对象,当我们需要修改这个对象状态,我们可以拷贝一个对象只修改需要修改的属性

        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<Forecast>)
data class Forecast(val dt: Long, val temp: Temperature, val pressure: Float, val humidity: Int, val weather: List<Weather>, 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<Forecast>): List<ModelForecast> {
        //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<T> {
    fun execute(): T
}

class RequestForecastCommand(val zipCode: String) : Command<ForecastList> {
    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<ForecastListAdapter.ViewHolder>() {
    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)
            }
        }

最后的运行效果:


png1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,324评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,173评论 25 709
  • 易效能6.0第4周周总结: 个人使命: 总纲:1.健康第一,睡眠充足,适当运动,绿色饮食;2.工作和生活平衡;3....
    张晨阳_ccd9阅读 1,001评论 0 0
  • 这一段时间以来,因为安排了晚班,就一直没有加过班。至今为止已经半个月了。 这半个月以来,去了一次长沙,吃了一顿饭。...
    孤寂于黑夜的浪子阅读 793评论 0 0
  • 01 ——你还爱我吗? ——我们现在不是朋友吗? ——你这个定义很完美。 以上微信对话来自于谢小姐与她的前男友,时...
    半岛同学阅读 3,226评论 12 13