Activity.setTitle不runOnUiThread块工作

问题描述 投票:0回答:2

我需要从后台线程从我的房间数据库中获取一个Note()对象,并设置说明,作为我的活动标题的标题,但title = note.title不工作,我在工具栏上看到我的应用程序名称。我也曾尝试supportActionBar?.titletoolbar.title但它们都没有解决的问题。我敢肯定,该数据库是给我正确的数据,我不知道哪里出了问题。任何帮助表示赞赏。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_show_note)
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onResume() {
    super.onResume()
    intent.extras?.also {
        val id = it.getInt(ID_EXTRA)
        Thread(Runnable {
            note = db.noteDao().getNote(id)
            runOnUiThread {
                title = note.title
                tvShowNote.text = note.note
                tvShowTime.text = note.time.format()
            }
        }).start()
    }
}
android android-actionbar android-titlebar
2个回答
1
投票

必须设置这样的事情。

supportActionBar!!.title = title //your_title_put_here

你试过没有runOnUiThread相同。


0
投票

是的setTitle操作栏的方法,你需要使用ActionBar的实例来设置其属性。尝试使用下面的代码你已经确立了自己的工具栏为支持动作条以后。

val actionBar = supportActionBar
actionBar!!.title = "your_title"
© www.soinside.com 2019 - 2024. All rights reserved.