I was trying to run a code
LiveData<ArrayList<TodoModel>> liveData = todoTable.getAllTodos(db);
todoTable.getAllTodos(db).observe(this, new Observer<ArrayList<TodoModel>>() {
@Override
public void onChanged(ArrayList<TodoModel> todoModels) {
todoList.clear();
todoList.addAll(todoModels);
todoAdapter.notifyDataSetChanged();
}
});
Here is the getAllTodos method
public MutableLiveData<ArrayList<TodoModel>> getAllTodos(SQLiteDatabase db) {
MutableLiveData<ArrayList<TodoModel>> liveTodos = new MutableLiveData<>();
ArrayList<TodoModel> todos = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME,
new String[]{columns.id, columns.title, columns.task, columns.category, columns.date, columns.time},
null, null, null,
null, null);
while (cursor.moveToNext()) {
TodoModel todo = new TodoModel(cursor.getString(1), cursor.getString(2),
cursor.getString(3), cursor.getLong(4), cursor.getLong(5));
todos.add(todo);
}
liveTodos.setValue(todos);
return liveTodos;
}
Whenver there is a change in ArrayList todos, the change is not shown immediately. But after restarting the app the new TodoModel is shown.
question from:
https://stackoverflow.com/questions/65920791/live-data-not-showing-android-studio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…