Custom search implementation with SearchView

后端 未结 2 1775
离开以前
离开以前 2021-01-18 22:29

I want to implement search in my app, but I don\'t want to use a separate Activity to display my search results. Instead, I only want to use the suggestionslist that display

2条回答
  •  灰色年华
    2021-01-18 23:12

    I have implemented search in my application by using an EditText which takes search string.
    And below this EditText I have my ListView on which I want to perform search.

    
    
    
      
    

    The list below the search EditText changes according to what search text is entered in the EditText.

    etSearch = (EditText) findViewById(R.id.searchInput);
    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            searchList();
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    });  
    

    The function searchList() does the actual searching

      private void searchList() {
        String s = etSearch.getText().toString();
        int textlength = s.length();
        String sApp;
        ArrayList appsListSort = new ArrayList();
        int appSize = list.size();
        for (int i = 0; i < appSize; i++) {
            sApp = list.get(i);
            if (textlength <= sApp.length()) {
                if (s.equalsIgnoreCase((String) sApp.subSequence(0, textlength))) {
                    appsListSort.add(list.get(i));
                }
            }
        }
        list.clear();
        for (int j = 0; j < appsListSort.size(); j++) {
            list.add(appsListSort.get(j));
        }
        adapter.notifyDataSetChanged();
    }  
    

    here list is the ArrayList which shows in the ListView and adapter is the ListView adapter.
    I hope this helps you in some way.

提交回复
热议问题