How to use text from database column in MainActiviy

20 Views Asked by At

In my app, the user clicks a FAB button to open SecondMainActivity. I need to find a way to prevent the user from opening SecondMainActivity UNLESS they have updated the database. I've written my code so that if the timeStamp from the database is the same as todaysDate, clicking the fabCountDay will open SecondMainActivity. If not the same, a snackbar will display.

Problem: how do I get text from the database timeStamp column into MainActivity?

Here's the code.

MainActivity ...

activityMainBinding!!.fabCountDay.setOnClickListener {

   val current = LocalDateTime.now()
   val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
   var todaysDate: String = current.format(formatter)
--->   val timeStamp = text from timeStamp column in the database <---

   if (todaysDate == timeStamp) {
       val intent = Intent(this@MainActivity, SecondMainActivity::class.java)
       startActivity(intent)
       this.finish()
   } else {
       val text = "Update a bird before opening Count Day"
       val snackbar = Snackbar.make(it, text, Snackbar.LENGTH_LONG)
       val view = snackbar.view
       val params = view.layoutParams as FrameLayout.LayoutParams
       params.gravity = Gravity.RIGHT
       view.layoutParams = params
       snackbar.show()
   }
}

...

0

There are 0 best solutions below