为什么在搜索项目期间我的回收商视图适配器清除?

我想从回收站视图中创建搜索项目。我有 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...();),当他搜索另一个时,他会得到新的搜索项目列表,而没有旧的搜索项目。但是我什么都没有!!!请帮帮我!我希望我能告诉你我的问题,你能理解我:)


万千封印
浏览 166回答 1
1回答

慕田峪4524236

目前还不清楚您的问题究竟是什么,但从我的想法来看:每次用户键入一个新字符时,onTextChanged都会触发另一个事件,并且由于您没有清除中间的适配器,显然您的addTask输入会累积。afterTextChanged被调用 after onTextChanged,因此如果您在填充适配器后立即清除它,它将最终为空。我不确定是notifyDataSetChanged立即重绘 RecyclerView 还是将其排队,但很可能另一个 notifyDataSetChanged 可能会在此期间adapter.clearAll或其他地方潜入,让您留下空视图。您应该在重新填充之前立即清除适配器,而不是之后立即清除。您不需要notifyDataSetChanged在每个addTask. 添加当前迭代的所有任务,然后调用notify...一次。如果您继续阅读,ontextChanged您会看到,count参数中仅显示更改了多少个字符(或者更确切地说,当前键入的单词或 smth 中有多少个字符),使用它来检测是否CharSequence s为空并不是最佳选择。而是检查CharSequence s.所以你需要做的是:   @Override    public void onTextChanged (CharSequence s, int start, int before, int count) {        if (s.length() == 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            adapter.clearAll();            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")));                 } while (cursor.moveToNext());            }            cursor.close();            adapter.notifyDataSetChanged();        }    }如果您的适配器按我的预期工作,那就应该这样做。你永远不表明您RecyclerView适配器,所以我不知道什么addTask和clearAll做。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java