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()
)