Search in sources :

Example 1 with SearchItem

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

the class BaseActivity method getData.

// ---------------------------------------------------------------------------------------------
@CallSuper
protected void getData(String text, int position) {
    mHistoryDatabase.addItem(new SearchItem(text));
    Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
    intent.putExtra(EXTRA_KEY_VERSION, SearchView.VERSION_TOOLBAR);
    intent.putExtra(EXTRA_KEY_VERSION_MARGINS, SearchView.VERSION_MARGINS_TOOLBAR_SMALL);
    intent.putExtra(EXTRA_KEY_THEME, SearchView.THEME_LIGHT);
    intent.putExtra(EXTRA_KEY_TEXT, text);
    startActivity(intent);
    Toast.makeText(getApplicationContext(), text + ", position: " + position, Toast.LENGTH_SHORT).show();
}
Also used : Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) SearchItem(com.lapism.searchview.SearchItem) CallSuper(android.support.annotation.CallSuper)

Example 2 with SearchItem

use of com.lapism.searchview.SearchItem 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

SearchItem (com.lapism.searchview.SearchItem)2 Intent (android.content.Intent)1 RecognizerIntent (android.speech.RecognizerIntent)1 CallSuper (android.support.annotation.CallSuper)1 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 SearchView (com.lapism.searchview.SearchView)1 ArrayList (java.util.ArrayList)1