0

I need to get a Note() object from my room database from a background thread and set the title of the note as my activity title, but title = note.title doesn't work and I see my application name in the toolbar. I have also tried supportActionBar?.title and toolbar.title but none of them solved the issue. I'm sure that the database is giving me the right data and I don't know where is the problem. Any help is appreciated.

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()
    }
}
Molly
  • 1,887
  • 3
  • 17
  • 34
Soroush Lotfi
  • 71
  • 1
  • 5
  • Where in your code are you changing your `toolbar` title? – Zun Feb 06 '19 at 10:57
  • @ZUNJAE `Thread(Runnable { note = db.noteDao().getNote(id) runOnUiThread { title = note.title } }).start()` – Soroush Lotfi Feb 06 '19 at 10:59
  • @ZUNJAE in onResume() – Soroush Lotfi Feb 06 '19 at 11:00
  • room by default runs query off the main thread, you can safely use live data returned to update ui elements – karan Feb 06 '19 at 11:01
  • @KaranMer `room by default runs query off the main thread` do you have any proof for that? You need to use LiveData and attach a listener yourself. By default the queries are on the main thread. – Zun Feb 06 '19 at 11:02
  • @SoroushLotfi I can't see where you change the title. You're only calling `title = note.title`. What is `title`? https://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shown – Zun Feb 06 '19 at 11:02
  • @ZUNJAE Karan Mer is right. If you put the queries in `init {}` block in `ViewModel` they run off the main thread. – Soroush Lotfi Feb 06 '19 at 11:04
  • @ZUNJAE This is Kotlin dude not Java. `title` in kotlin means `setTitle()` in Java – Soroush Lotfi Feb 06 '19 at 11:04
  • refer the docs https://developer.android.com/training/data-storage/room/accessing-data – karan Feb 06 '19 at 11:05
  • @KaranMer You're right I can use livedata. Thanks for reminding. But I still doesn't understand why `title = ""` doesn't work in my code. – Soroush Lotfi Feb 06 '19 at 11:07
  • also setTitle is method of support action bar, calling title will simply understand is activity's param which is not the case. – karan Feb 06 '19 at 11:07
  • You need to `set title` for the toolbar. As such, the code should be `(your_action_bar).title` – Zun Feb 06 '19 at 11:10
  • Room only allows query on the main thread if you turn on `allowMainThreadQueries`. – Zun Feb 06 '19 at 11:12

2 Answers2

1

You have to set something like this.

supportActionBar!!.title = title //your_title_put_here

Have you tried the same without runOnUiThread.

0

setTitle is method of action bar, you need to use actionbar's instance to set its property. try using below code after you have set your toolbar as support actionbar.

val actionBar = supportActionBar
actionBar!!.title = "your_title"
karan
  • 8,637
  • 3
  • 41
  • 78