Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

database - Live Data Not showing (Android Studio)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...