是否有一种干净的DRY方式来从HTTP请求JSON更新多个textView?

时间:2017-02-15 08:39:26

标签: json kotlin anko

我有以下Kotlin / Anko / Android活动。

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.kittinunf.fuel.Fuel
import eu.gwapi.laaketilaus.util.JSON
import eu.gwapi.laaketilaus.util.Order
import org.jetbrains.anko.find
import org.jetbrains.anko.textView
import org.jetbrains.anko.toast
import org.jetbrains.anko.verticalLayout

class OrderDetailsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val order_id: Long = intent.extras.getLong("order_id")
        verticalLayout {
            textView {
                id = R.id.order_detail_customer
            }
            textView {
                id = R.id.order_detail_address
            }
            textView {
                id = R.id.order_detail_postal_code
            }
            textView {
                id = R.id.order_detail_phone
            }
        }
        getOrder(order_id)
    }

    fun getOrder(order_id: Long) {
        Fuel.get("https://my.api.endpoint/" + order_id.toString()).responseString { request, response, result ->
            val (json, err) = result
            if (err != null) {
                toast(err.toString())
            } else {
                val order: Order = JSON.readValue(json!!)
                find<TextView>(R.id.order_detail_customer).text = order.customer
                find<TextView>(R.id.order_detail_address).text = order.address
                find<TextView>(R.id.order_detail_postal_code).text = order.postal_code
                find<TextView>(R.id.order_detail_phone).text = order.phone
            }
        }
    }
}

对于像我这样顽固的pythonista来说,这似乎是非常静态和冗长的方式。

有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

由于只有TextView,您只需要更改其文本,您可以通过以下方式简化代码:

  • 添加存储ID的Order属性的映射:

    private val orderPropertyToTextViewId = mapOf(
            Order::customer to R.id.order_detail_customer,
            Order::address to R.id.order_detail_address,
            Order::postalCode to R.id.order_detail_postal_code,
            Order::phone to R.id.order_detail_phone
    )
    
  • 创建迭代地图的视图:

    verticalLayout {
        for ((property, textViewId) in orderPropertyToTextViewId) {
            textView { id = textViewId }
        }
    }
    
  • 更新迭代地图的文字:

    for ((property, textViewId) in orderPropertyToTextViewId) {
        findViewById<TextView>(textViewId).text = property.get(order)
    }
    

如果您存储findViewById<TextView>(...)次调用返回的TextView而不是地图中的ID,那么您可以更进一步摆脱ID和textView { ... },但这需要进一步试验。

答案 1 :(得分:1)

如果您不需要经常刷新新数据,则不需要保留对TextView的引用。我不使用Anko,但它可能看起来像:

val order: Order = JSON.readValue(json!!)
verticalLayout {
    arrayOf(order.customer, order.address, order.postal_code, order.phone)
            .map {
                textView {
                    text = it
                }
            }
}