【发布时间】:2021-12-18 01:45:25
【问题描述】:
我正在尝试解决我的问题。当我制作一些基本的天气应用程序时。当我打开新活动时,我可以将城市添加到收藏列表。问题是当我按下后退按钮时,我需要刷新滚动视图,但我不知道如何。我试过 onBackPressed 但它不起作用。
这是部分代码
fun showWeather(searchedCity: String?) {
var city: CityObject
if (searchedCity.isNullOrEmpty()) {
Toast.makeText(applicationContext, "NEED TO WRITE CITY!", Toast.LENGTH_LONG).show()
}
// Need have threat cause internet
thread = Thread {
// getting data
var jsonData = jsonParser.getJsonData("$searchedCity")
if (!jsonData.isNullOrEmpty()) {
//parsing data
city = jsonParser.parseJsonData(jsonData)!!
// for start another activity
startActivity(city)
}
}
thread.start()
textInputEditText.text?.clear()
}
//showing weather
fun startActivity(city: CityObject) {
runOnUiThread {
val intent = Intent(this, WeatherActivity::class.java)
intent.putExtra("CITY_OBJECT", city)
startActivity(intent)
}
}
// making favourite cities buttons
fun getFavouriteCities() {
linInScroll.removeAllViews()
linInScroll.setOrientation(LinearLayout.VERTICAL);
for (cityName in DB.getData()) {
val button = Button(this)
button.setText("$cityName")
button.setTextSize(1, 20F)
button.setOnClickListener {
showWeather("$cityName")
}
linInScroll.addView(button)
}
}
最喜欢的城市列表
感谢您的帮助
【问题讨论】:
-
您好,您可以尝试在 onResume() 方法中调用 getFavouriteCities()。
-
太棒了!有用!非常感谢你!
-
如果
onResume对您有好处,那很酷(请记住,每次应用程序进入视图时,当显示屏旋转等时都会调用它)但是处理后退按钮的正确方法是添加OnBackPressedCallback:developer.android.com/guide/navigation/navigation-custom-back
标签: android multithreading kotlin