androidsqliteandroid-recyclerviewadapter

Why do the recycler items disappear when the search box is empty?


I created a to do list app, when I add some tasks, they are displayed correctly in Recycler. Now, after adding the tasks, I want to search. When I fill the search box with characters, it searches the tasks correctly, but as soon as the search box is empty (its length becomes zero), all the previously existing lists cannot be displayed, but if they exist in the database

AdapterClass

// To set the list of tasks class inside the adapter to display the tasks
public void setTask(List<TaskModel> task) {
    this.taskModel = task;
    notifyDataSetChanged();
}

SQLiteDataBaseClass

// This method is the search command inside the SQLite class
public List<TaskModel> searchBox(String query) {
    List<TaskModel> taskModel = new ArrayList<>();
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_TASK + " WHERE title LIKE '%" + query + "%'", null);
    if (cursor.moveToFirst()) {
        do {
            TaskModel task = new TaskModel();
            task.setId(cursor.getLong(0));
            task.setTitle(cursor.getString(1));
            task.setCompleted(cursor.getInt(2) == 1);
            taskModel.add(task);
        } while (cursor.moveToNext());
    }
    sqLiteDatabase.close();
    return taskModel;
}

// Method to return list of tasks in SQLite class
public List<TaskModel> getTask() {
    List<TaskModel> taskModel = new ArrayList<>();
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_TASK, null);
    if (cursor.moveToFirst()) {
        do {
            TaskModel task = new TaskModel();
            task.setId(cursor.getLong(0));
            task.setTitle(cursor.getString(1));
            task.setCompleted(cursor.getInt(2) == 1);
        } while (cursor.moveToNext());
    }
    sqLiteDatabase.close();
    return taskModel;
}

MainActivity

public void setSearchBox() {
    searchBox.addTextChangedListener(new TextWatcher() {
        
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 0) {
                List<TaskModel> task = sqLite.searchBox(s.toString());
                adapter.setTask(task);
            } else {
                List<TaskModel> task = sqLite.getTask();
                adapter.setTask(task);
            }
        }
       
        @Override
        public void afterTextChanged(Editable s) {
        
        }
    });
}

Solution

  • After reviewing your code closely I found one issue in getTask() method. You didn't added data fetched from database in list taskModel List<TaskModel> and you pass empty list to the adapter that's why you have data in database but not showing in RecyclerView.

    Update getTask() method to this:

    // Method to return list of tasks in SQLite class
    public List<TaskModel> getTask() {
        List<TaskModel> taskModel = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_TASK, null);
        if (cursor.moveToFirst()) {
            do {
                TaskModel task = new TaskModel();
                task.setId(cursor.getLong(0));
                task.setTitle(cursor.getString(1));
                task.setCompleted(cursor.getInt(2) == 1);
                taskModel.add(task);  // <<--- ADD THIS LINE 
            } while (cursor.moveToNext());
        }
        sqLiteDatabase.close();
        return taskModel;
    }