Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一种干净的方法可以从HTTP请求JSON更新多个文本视图?_Json_Kotlin_Anko - Fatal编程技术网

是否有一种干净的方法可以从HTTP请求JSON更新多个文本视图?

是否有一种干净的方法可以从HTTP请求JSON更新多个文本视图?,json,kotlin,anko,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.u

我有以下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
            }
        }
    }
}
导入android.os.Bundle
导入android.support.v7.app.AppActivity
导入android.widget.TextView
导入com.fasterxml.jackson.module.kotlin.readValue
导入com.github.kittinunf.fuel.fuel
导入eu.gwapi.laaketilaus.util.JSON
导入eu.gwapi.laaketilaus.util.Order
导入org.jetbrains.anko.find
导入org.jetbrains.anko.textView
导入org.jetbrains.anko.toast
导入org.jetbrains.anko.verticalLayout
类OrderDetailsActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
val order\u id:Long=intent.extras.getLong(“order\u id”)
垂直布局{
文本视图{
id=R.id.订单\详细信息\客户
}
文本视图{
id=R.id.订单\详细信息\地址
}
文本视图{
id=R.id.订单\详细信息\邮政编码
}
文本视图{
id=R.id.订单\详细信息\电话
}
}
getOrder(订单号)
}
fun getOrder(订单号:Long){
燃料。获取(“https://my.api.endpoint/“+order_id.toString()).responseString{请求、响应、结果->
val(json,err)=结果
if(err!=null){
toast(err.toString())
}否则{
val order:order=JSON.readValue(JSON!!)
查找(R.id.order\u detail\u customer)。text=order.customer
查找(R.id.order\u detail\u address)。text=order.address
查找(R.id.order\u detail\u postal\u code)。text=order.postal\u code
查找(R.id.order\u detail\u phone)。text=order.phone
}
}
}
}
对于像我这样的顽固的蟒蛇主义者来说,这似乎是一种非常静态和冗长的方式


有更好的方法吗?

因为只有
TextView
s,您只需要更改它们的文本,所以可以用以下方法简化代码:

  • 为存储ID的
    订单
    属性添加映射:

    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)
    }
    
  • 更新在地图上迭代的文本:

    verticalLayout {
        for ((property, textViewId) in orderPropertyToTextViewId) {
            textView { id = textViewId }
        }
    }
    
    for ((property, textViewId) in orderPropertyToTextViewId) {
        findViewById<TextView>(textViewId).text = property.get(order)
    }
    
    orderPropertyToTextViewId中((属性,textViewId)的
    {
    findViewById(textViewId).text=property.get(订单)
    }
    

如果存储由
TextView{…}
调用返回的
TextView
而不是映射中的id,则可以更进一步地删除id和
findViewById(…)
,但这需要进一步试验。

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

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

orderPropertyToTextViewId.forEach{property,textViewId->…}
以获得比依赖
for
循环更实用(更惯用)的代码