【问题标题】:Android Update View after JSONRequest successJSONRequest成功后的Android更新视图
【发布时间】:2016-10-29 17:02:09
【问题描述】:

我正在构建一个应该使用 OpenWeather API 显示天气信息的 Android 应用程序(我使用的是 Kotlin 而不是 JAVA,但如果你能告诉我 Java 方法很好)。我已经设置好了视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layCity">
        <TextView
            android:id="@+id/txtCity"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="PORTO"
            android:textSize="25sp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/txtCountry"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="(PT)"
            android:textSize="25sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layTempDetail"
        android:layout_below="@+id/layCity"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtCurTemp"
            android:text="20º"
            android:textSize="40sp"
            android:textStyle="bold"
            android:layout_marginTop="20dp"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lblMinTemp"
                android:text="Min:"
                android:textStyle="bold"
                android:textSize="20sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtMinTemp"
                android:text="10º"
                android:textSize="20sp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lblMaxTemp"
                android:text="Max:"
                android:textStyle="bold"
                android:textSize="20sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtMaxTemp"
                android:text="30º"
                android:textSize="20sp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lblPressure"
                android:text="Pressure:"
                android:textStyle="bold"
                android:textSize="20sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtPressure"
                android:text="100"
                android:textSize="20sp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lblHumidity"
                android:text="Humidity:"
                android:textStyle="bold"
                android:textSize="20sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtHumidity"
                android:text="30%"
                android:textSize="20sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

然后我有另一个类负责发出请求并单独更改每个字段,如下所示:

public fun getCityInfo(name: String){
        val URI = String.format("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=", name)
        Log.d("uri", URI)
        var cityName: String = ""
        queue.add(JsonObjectRequest(
                URI,
                null,
                {

                    cityName = it.getString("name")
                    var main = it.getJSONObject("main")
                    var temp: Double = main.getDouble("temp")
                    //getting the rest of the info


//I save the info that I need in a class

                    var currTemp = CurrentTemperature(temp,press,humidity,min,max,state,desc,icon)
                    var city = City(name, currTemp)


//then set each field individually
                    var detailsview = (context as AppCompatActivity).findViewById(R.id.layDetails) as View

                    (detailsview.findViewById(R.id.txtCity) as TextView).setText(city.name)
                    (detailsview.findViewById(R.id.txtCurTemp) as TextView).setText(city.currTemp.curr.toString())
},
                {
                    Toast.makeText(context, "Failed to get weather", Toast.LENGTH_LONG).show()
                }

但我想要的是在成功时返回城市实例,然后我的字段中有某种绑定会自动更新视图。

我怎样才能做到这一点?

【问题讨论】:

  • 你为什么有一个单独的类来做请求?
  • 此外,除非您告诉它,否则 volley 不会自动刷新或绑定到任何视图。例如,您已经在setText(city.name) 完成了。不太确定我是否需要var city
  • @cricket_007 仅出于组织原因。仅此而已。我是 Android 的新手,所以这就是我想知道的,如果有办法更新实例让我们说,那么视图会刷新
  • 如果您在我的视图示例中看到初始值是手动设置的

标签: java android kotlin


【解决方案1】:

根本不了解 Kotlin,但我会尝试使用 Java 术语进行足够的解释。

重新定义您的方法以接受 Volley 侦听器作为 Activity 的回调。

public fun getCityInfo(name: String, callback: Response.Listener<JSONObject>)

然后,您调用该函数,将这个闭包作为 callback 传入

getCityInfo("Chicago", 
{
    var cityName = it.getString("name")
    var main = it.getJSONObject("main")
    var temp: Double = main.getDouble("temp")
    //getting the rest of the info

    var currTemp = CurrentTemperature(temp,press,humidity,min,max,state,desc,icon)
    var city = City(name, currTemp)

    setCity(city) // TODO: implement method within Activity 
}) 

注意添加了setCity。您需要实现它以更新您拥有的任何视图。

现在,在另一个类中,只需传递来自 Activity 的响应侦听器

queue.add(JsonObjectRequest(
            URI,
            null, 
            callback, 
            { ... } // error handle 

【讨论】:

  • 我的问题是,如何将该城市实例字段绑定到视图中的“TextView”字段?
  • (findViewById(R.id.txtCity) as TextView).setText(city.name)...对吗?
  • 如果您想询问这个数据绑定库,我无能为力。 developer.android.com/topic/libraries/data-binding/index.html
  • 我知道,我在问是否有办法自动完成。或者那是更有效的一种。我问这个是因为我通常使用 ASP.Net MVC 和 WPF,你几乎可以通过绑定来完成所有工作。未设置字段
  • 好的,是的,如果你进行比较,你只需要我链接到的那个库。尽管如此,这种定义回调的方法将允许您更轻松地获取对您调用的天气服务的 Activity 类的引用
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多