Search in sources :

Example 1 with SearchHistoryTable

use of com.lapism.searchview.SearchHistoryTable in project SearchView by lapism.

the class BaseActivity method setSearchView.

// it can be in OnCreate
protected void setSearchView() {
    mHistoryDatabase = new SearchHistoryTable(this);
    mSearchView = (SearchView) findViewById(R.id.searchView);
    if (mSearchView != null) {
        mSearchView.setHint(R.string.search);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                getData(query, 0);
                mSearchView.close(false);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        mSearchView.setOnOpenCloseListener(new SearchView.OnOpenCloseListener() {

            @Override
            public boolean onOpen() {
                if (mFab != null) {
                    mFab.hide();
                }
                return true;
            }

            @Override
            public boolean onClose() {
                if (mFab != null && !mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mFab.show();
                }
                return true;
            }
        });
        mSearchView.setVoiceText("Set permission on Android 6.0+ !");
        mSearchView.setOnVoiceClickListener(new SearchView.OnVoiceClickListener() {

            @Override
            public void onVoiceClick() {
            // permission
            }
        });
        List<SearchItem> suggestionsList = new ArrayList<>();
        suggestionsList.add(new SearchItem("search1"));
        suggestionsList.add(new SearchItem("search2"));
        suggestionsList.add(new SearchItem("search3"));
        SearchAdapter searchAdapter = new SearchAdapter(this, suggestionsList);
        searchAdapter.addOnItemClickListener(new SearchAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                TextView textView = (TextView) view.findViewById(R.id.textView_item_text);
                String query = textView.getText().toString();
                getData(query, position);
                mSearchView.close(false);
            }
        });
        mSearchView.setAdapter(searchAdapter);
    /*suggestionsList.add(new SearchItem("search12"));
            suggestionsList.add(new SearchItem("search22"));
            suggestionsList.add(new SearchItem("search32"));
            searchAdapter.notifyDataSetChanged();*/
    /*
            List<SearchFilter> filter = new ArrayList<>();
            filter.add(new SearchFilter("Filter1", true));
            filter.add(new SearchFilter("Filter2", true));
            mSearchView.setFilters(filter);
            //use mSearchView.getFiltersStates() to consider filter when performing search
            */
    }
}
Also used : SearchAdapter(com.lapism.searchview.SearchAdapter) ArrayList(java.util.ArrayList) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) SearchView(com.lapism.searchview.SearchView) TextView(android.widget.TextView) SearchItem(com.lapism.searchview.SearchItem) SearchView(com.lapism.searchview.SearchView) TextView(android.widget.TextView) SearchHistoryTable(com.lapism.searchview.SearchHistoryTable)

Aggregations

NavigationView (android.support.design.widget.NavigationView)1 View (android.view.View)1 TextView (android.widget.TextView)1 SearchAdapter (com.lapism.searchview.SearchAdapter)1 SearchHistoryTable (com.lapism.searchview.SearchHistoryTable)1 SearchItem (com.lapism.searchview.SearchItem)1 SearchView (com.lapism.searchview.SearchView)1 ArrayList (java.util.ArrayList)1