There is a SearchView which displays values ​​from the database. How to install a handler for clicking on the values ​​that SearchView displays, for example, to switch to an Activity of this value?

enter image description here

Main Activity

public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.menu_main, menu); activateSearchView(menu); return true; } private void activateSearchView(Menu menu) { MenuItem searchItem = menu.findItem(R.id.search); searchView = (SearchView) MenuItemCompat.getActionView(searchItem); final SearchManager searchManager = (SearchManager) getSystemService (Context.SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, MainActivity.class))); } 

Database helper

  public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) { return myDataBase.query("NATURE_TABLE", null, null, null, null, null, null); } public ArrayList<String> getAllEmployeesName(){ SQLiteDatabase db = this.getReadableDatabase(); ArrayList<String> employeesNames = new ArrayList<String>(); Cursor cursor = db.query("NATURE_TABLE",null,null,null,null,null,null,null); if(cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); do{ String name = cursor.getString(1); employeesNames.add(name); }while(cursor.moveToNext()); } cursor.close(); db.close(); return employeesNames; } 

ProviderActivity

 public class ProviderActivity extends ContentProvider { DatabaseHelper myDB; ArrayList<String> employees; @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) { myDB = new DatabaseHelper(getContext()); String employeeName; employees = myDB.getAllEmployeesName(); MatrixCursor matrixCursor = new MatrixCursor(new String[]{ BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID }); if(employees != null){ String query = uri.getLastPathSegment().toString(); int limit = Integer.parseInt(uri.getQueryParameter (SearchManager.SUGGEST_PARAMETER_LIMIT)); int length = employees.size(); for (int i = 0; i < length && matrixCursor.getCount() < limit; i++) { String employee = employees.get(i); if(employee.contains(query)) { matrixCursor.addRow(new Object[]{i, employee, i}); } } } return matrixCursor; } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues contentValues) { return null; } @Override public int delete(Uri uri, String s, String[] strings) { return 0; } @Override public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { return 0; } 
  • Do you need to switch to different activites for different search strings or to transfer to the same one, which is chosen during the search? What happens next when something is selected in the search? - pavlofff
  • For each line its own activit. That is, I am looking for the “Eye”, I find it and I need to switch to activating the Eyes - EyesActivity. @pavlofff - Leo Naumenko

1 answer 1

If I understand you correctly, then so:

 SearchView searchView = (SearchView) findViewById(R.id.searchView); searchView.setOnSearchClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); } }); 

Also here is a link to the documentation.

  • one
    For each record, you need to switch to your activations, and not to one for all - pavlofff
  • I understand that you gave a code in which, when selecting any result in SearchView, it jumps to the second activation. But I need to go to the activation of the value that I found in SearchView. For example, above I have a screen where I enter the "Eye" in SeachVIew, it finds it in the database, and I have to go to the line with the "Eye" that SeacrView found on the Eye, for example, EyesActivity Further, I find the Brain, I click on it and I should be transferred to BrainActivity. - Lev Naumenko
  • But this way you can transfer data to another Activity using putExtra . I understand how the layout second activity is typical for displaying information? If not, then you can divide all the search results into specific groups, and then work with them through the switch . The question does not arise in the design and arrangement of controls, as I understand it, but the question is how to display information. - Sergey Neykovich