Custom search implementation with SearchView

后端 未结 2 1748
离开以前
离开以前 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:11

    What you need to create is a Content Provider. In that way you can add custom results to your SearchView and add the autocomplete feature to it whenever the User inputs something.

    If I don't remember incorrectly, in one of my projects I have done something similar, and it didn't take too long.

    I believe this could be helpful: Turn AutoCompleteTextView into a SearchView in ActionBar instead

    And also this: SearchManager - adding custom suggestions

    Hope this helps.

    N.

    0 讨论(0)
  • 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.

    <EditText
        android:id="@+id/searchInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/input_patch"
        android:gravity="center_vertical"
        android:hint="@string/search_text"
        android:lines="1"
        android:textColor="@android:color/white"
        android:textSize="16sp" >
    </EditText>
    <ListView
        android:id="@+id/appsList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/searchInput"
        android:cacheColorHint="#00000000" >
    </ListView>  
    

    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<String> appsListSort = new ArrayList<String>();
        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.

    0 讨论(0)
提交回复
热议问题