簡體   English   中英

為什么在搜索項目時清除回收者視圖適配器?

[英]Why my recycler view adapter clearing during search for items?

我想從回收者視圖中搜索物品。 我在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 ...(););當他要搜索另一個數據時,他會得到新的搜索項目列表,而沒有舊的搜索項目。 但是我什么都沒有!!! 請幫我! 希望我能告訴您我的問題,並且您可以理解我:)

目前還不清楚您的問題到底是什么,但是從我的想法來看:

  • 每次用戶鍵入一個新字符時, onTextChanged觸發另一個onTextChanged事件,並且由於您沒有清除中間的適配器,顯然addTask條目會累積。
  • afterTextChangedonTextChanged之后被調用,因此,如果在填充適配器后onTextChanged清除適配器,則適配器最終為空。 我不確定notifyDataSetChanged是否notifyDataSetChanged重繪RecyclerView或將其排隊,但是很可能另一個notifyDataSetChanged可能在adapter.clearAll或其他地方潛行,從而使您的視圖空着。 您應該重新填充適配器之前立即清除適配器,而不是緊接之后。
  • 你不需要調用notifyDataSetChanged每之后addTask 添加當前迭代的所有任務,然后調用notify...一次。
  • 如果您閱讀ontextChanged會發現,參數中的count僅顯示更改了多少個字符(或者當前鍵入的單詞或文字中有多少個字符),這不是使用它來檢測CharSequence s最佳選擇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適配器,所以我不知道addTaskclearAll做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM