第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在搜索項目期間我的回收商視圖適配器清除?

為什么在搜索項目期間我的回收商視圖適配器清除?

萬千封印 2021-12-10 12:33:07
我想從回收站視圖中創(chuàng)建搜索項目。我有 MainActivity,我有主回收器視圖,我有 SearchActivity,我有秒回收器視圖,用于顯示搜索項目。當用戶在輸入中輸入一個字母時,我會向我的 SQLite 詢問與輸入文本相同的查詢。如果我的 SQLite 給了我數(shù)據(jù),我會將這些數(shù)據(jù)插入到第二個回收器視圖中并顯示給用戶。這是我的代碼:// 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 方法中調(diào)用adapter.clearAll();。我希望當用戶完成輸入他的數(shù)據(jù)時,這些數(shù)據(jù)添加到適配器中,適配器清除而不更新回收器視圖(notifyDataSet...();),當他搜索另一個時,他會得到新的搜索項目列表,而沒有舊的搜索項目。但是我什么都沒有?。?!請幫幫我!我希望我能告訴你我的問題,你能理解我:)
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經(jīng)驗 獲得超5個贊

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

  • 每次用戶鍵入一個新字符時,onTextChanged都會觸發(fā)另一個事件,并且由于您沒有清除中間的適配器,顯然您的addTask輸入會累積。

  • afterTextChanged被調(diào)用 after onTextChanged,因此如果您在填充適配器后立即清除它,它將最終為空。我不確定是notifyDataSetChanged立即重繪 RecyclerView 還是將其排隊,但很可能另一個 notifyDataSetChanged 可能會在此期間adapter.clearAll或其他地方潛入,讓您留下空視圖。您應該重新填充之前立即清除適配器,而不是之后立即清除。

  • 您不需要notifyDataSetChanged在每個addTask. 添加當前迭代的所有任務,然后調(diào)用notify...一次。

  • 如果您繼續(xù)閱讀,ontextChanged您會看到,count參數(shù)中僅顯示更改了多少個字符(或者更確切地說,當前鍵入的單詞或 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做。


查看完整回答
反對 回復 2021-12-10
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號