Example usage for android.widget SearchView SearchView

List of usage examples for android.widget SearchView SearchView

Introduction

In this page you can find the example usage for android.widget SearchView SearchView.

Prototype

public SearchView(Context context) 

Source Link

Usage

From source file:gov.cdc.epiinfo.RecordList.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = new SearchView(this);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override//from   ww  w  .ja  v  a  2  s  .  c o  m
        public boolean onQueryTextSubmit(String searchTerm) {

            try {
                mDbHelper.fetchTopOne();

                String query = BuildQuery(searchTerm);

                String fieldName1;
                String fieldName2;
                String fieldName3;
                String[] from = new String[1];
                int[] to = new int[1];

                if (formMetadata.DataFields.size() > 2) {

                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    fieldName2 = formMetadata.DataFields.get(1).getName();
                    fieldName3 = formMetadata.DataFields.get(2).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, fieldName2, fieldName3, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "columnName2", fieldName2,
                            "columnName3", fieldName3, "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.header3, R.id.text3,
                            R.id.header4, R.id.text4, R.id.hiddenText };
                } else if (formMetadata.DataFields.size() == 2) {
                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    fieldName2 = formMetadata.DataFields.get(1).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, fieldName2, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "columnName2", fieldName2,
                            "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.header3, R.id.text3,
                            R.id.hiddenText };
                } else if (formMetadata.DataFields.size() == 1) {
                    fieldName1 = formMetadata.DataFields.get(0).getName();
                    mNotesCursor = mDbHelper.fetchWhere(fieldName1, query);
                    from = new String[] { "_id", "columnName1", fieldName1, "_syncStatus" };
                    to = new int[] { R.id.text1, R.id.header2, R.id.text2, R.id.hiddenText };
                }

                startManagingCursor(mNotesCursor);
                CustomListAdapter notes = new CustomListAdapter(self, R.layout.line_list_row, mNotesCursor,
                        from, to);
                lineListFragment.setListAdapter(notes);

            } catch (Exception ex) {
                fillData();
            }

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if (newText.equals("")) {
                fillData();
            }

            return false;
        }
    });

    mnuSearch = menu.add(0, SEARCH_ID, 0, R.string.menu_search);
    mnuSearch.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    mnuSearch.setActionView(searchView);
    mnuSearch.setIcon(gov.cdc.epiinfo.R.drawable.action_search);

    MenuItem mnuQR = menu.add(0, QR_ID, 1, R.string.menu_barcode);
    mnuQR.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    mnuQR.setIcon(gov.cdc.epiinfo.R.drawable.qrcode_scan);

    MenuItem mnuCloud = menu.add(0, CLOUD_ID, 2, R.string.menu_cloud_sync);
    mnuCloud.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    MenuItem mnuSync = menu.add(0, SYNC_ID, 3, R.string.menu_sync_file);
    mnuSync.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    MenuItem mnuDeleteAll = menu.add(0, DELETE_ALL_ID, 4, R.string.menu_delete_all);
    mnuDeleteAll.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    if (fkeyGuid == null || fkeyGuid.length() == 0) {
        mnuSetDefault = menu.add(0, SET_DEFAULT_ID, 5, R.string.set_default_form);
        mnuSetDefault.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        mnuExitDefault = menu.add(0, EXIT_DEFAULT_MODE_ID, 5, R.string.exit_default_form);
        mnuExitDefault.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        if (AppManager.getDefaultForm().equals("")) {
            mnuSetDefault.setVisible(true);
            mnuExitDefault.setVisible(false);
        } else {
            mnuSetDefault.setVisible(false);
            mnuExitDefault.setVisible(true);
        }
    }

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    if (sharedPref.getString("cloud_service", "").equals("Box")) {
        mnuBoxSignin = menu.add(0, BOX_SIGNIN_ID, 6, R.string.box_signin);
        mnuBoxSignin.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        mnuBoxSignout = menu.add(0, BOX_SIGNOUT_ID, 6, R.string.box_signout);
        mnuBoxSignout.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        if (BoxClient.isAuthenticated(this)) {
            mnuBoxSignout.setVisible(true);
            mnuBoxSignin.setVisible(false);
        } else {
            mnuBoxSignout.setVisible(false);
            mnuBoxSignin.setVisible(true);
        }
    }

    MenuItem mnuHelp = menu.add(1, HELP_ID, 7, R.string.menu_help);
    mnuHelp.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;
}