我正在努力解決我的問題。當我制作一些基本的天氣應用程式時。當我打開新活動時,我可以將城市添加到收藏夾串列中。問題是當我按下后退按鈕時,我需要重繪 滾動視圖,但我不知道如何。我試過 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)
}
}
最喜歡的城市串列

感謝幫助
uj5u.com熱心網友回復:
每次您的活動進入前臺時onResume都會呼叫方法。
override fun onResume() {
super.onResume()
getFavouriteCities()
}
您可能還想查看其他生命周期事件。
請查看
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/349072.html
