我想从回收站视图中创建搜索项目。我有 MainActivity,我有主回收器视图,我有 SearchActivity,我有秒回收器视图,用于显示搜索项目。当用户在输入中输入一个字母时,我会向我的 SQLite 询问与输入文本相同的查询。如果我的 SQLite 给了我数据,我会将这些数据插入到第二个回收器视图中并显示给用户。这是我的代码:
// onCreate() method
initRecyclerView(); // init empty recycler view
EditText et_input = getActivity().findViewById(R.id.et_search_for_task_input);
et_input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
// nothing...
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
if (count == 0) { // if empty I show to user nothing
adapter.clearAll();
adapter.notifyDataSetChanged();
} else {
Cursor cursor = database.rawQuery("SELECT * FROM todo WHERE task LIKE '%" + String.valueOf(s) + "%'", null); // ask for data from database
if (cursor.moveToFirst()) { // if the response wasn't empty
do {
// add filter data in adapter and save adapter(notifyDataSetChanged();)
adapter.addTask(cursor.getString(cursor.getColumnIndex("task")));
adapter.notifyDataSetChanged();
} while (cursor.moveToNext());
}
cursor.close();
}
}
@Override
public void afterTextChanged (Editable s) {
// adapter.clearAll();
}
});
一切都很好。但我有这个问题。当用户输入一个字母时,他会得到一个搜索项目列表,如果他想添加或删除输入中的文本,他会得到一个带有旧搜索项目的新搜索项目列表。所以,我需要从适配器中删除旧的搜索项目。我该怎么做????我一直在尝试做下一个:在 afterTextChanged 方法中调用adapter.clearAll();。我希望当用户完成输入他的数据时,这些数据添加到适配器中,适配器清除而不更新回收器视图(notifyDataSet...();),当他搜索另一个时,他会得到新的搜索项目列表,而没有旧的搜索项目。但是我什么都没有!!!请帮帮我!我希望我能告诉你我的问题,你能理解我:)
万千封印
慕田峪4524236
随时随地看视频慕课网APP
相关分类