0

I have a list view in the home screen widget of my android app which fetches items from a database and then updates values in the listview. Since AsyncTask is deprecated, I have tried to use handler. The add item method outside the execute method is working, but the code inside post method(which is dependent on the fetched data) isn't working. Where am I going wrong?

val todosRemoteView = RemoteViews.RemoteCollectionItems.Builder()
val handler: Handler = Handler(Looper.getMainLooper())
val backgroundExecutor = Executors.newSingleThreadScheduledExecutor()

backgroundExecutor.execute {
    val db = Room.databaseBuilder(
        context,
        AppDatabase::class.java, "db"
    ).build()
    val todosDAO = db.TodoDAO()
    val todos: List<Todo> = todosDAO.getByTimeType(timeTypeHash[timeType]!!)
    
    handler.post {
        Log.d("debugging", "all the todos for ${timeTypeHash[timeType]} are $todos")
        for (todo in todos) {
            val view = RemoteViews(context.packageName, R.layout.each_todo).apply {
                setTextViewText(R.id.each_todo_container_text, todo.taskName)
                setCompoundButtonChecked(
                    R.id.each_todo_container_checkbox,
                    todo.finished!!
                )
            }
            todosRemoteView.addItem(
                todo.id.toString().toInt().toLong(),
                                    view
            )//this doesn't works
        }
        Log.d("debugging", "update is triggered");
    }
}

todosRemoteView.addItem(//this works
849938,
RemoteViews(context.packageName, R.layout.each_todo).apply {
    setTextViewText(R.id.each_todo_container_text, "random")
    setCompoundButtonChecked(
        R.id.each_todo_container_checkbox,
        false
    )
})
setRemoteAdapter(
    R.id.todos_list,
    todosRemoteView
    .build()
)
Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
Pro
  • 445
  • 4
  • 14
  • Does the `post` method of handler get executed? Did you see its logs? – Kozmotronik Dec 07 '22 at 13:02
  • yes, the post method's logs are visible – Pro Dec 08 '22 at 18:10
  • Can you enhance your codes in the question so that it can be used as a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and reproduce your problem? – Kozmotronik Dec 09 '22 at 06:56
  • @Kozmotronik please have a look at this https://stackoverflow.com/questions/74740967/cant-update-ui-asynchronously-with-co-routines – Pro Dec 09 '22 at 09:13
  • I did it @Pro, but still not much difference, still a reproducible example needed. For instance, what do you execute in `RemoteView.addItem` function? This kind of implementation doesn't seem to match the guidelines like MVVM. – Kozmotronik Dec 09 '22 at 13:14
  • In your implementation it looks like you instantiate a new item view everytime a `TODO` record is obtained from the database. But you must used an already instantiated one by a recycler view perhaps or whatever framework you use to list the `TODO` items. So instantiating items is your own buissness but instantiating views for items is not yours. It is the buissness of the underlaying framework to do so. I hope it is clear enough to understand. – Kozmotronik Dec 09 '22 at 13:20

0 Answers0