Posted By

azovotha on 03/07/13


Tagged

database search String android listview


Versions (?)

search string


 / Published in: Java
 

search a string inside the database an post the results into a ListView

  1. //in the dbHelper
  2. public Cursor searchStr(String str){
  3. SQLiteDatabase db = getReadableDatabase();
  4. Cursor cursor = db.rawQuery(" SELECT _id FROM "+Vals.PAGES_TABLE_NAME+" WHERE content LIKE '%"+str+"%'", null);
  5. return cursor;
  6. }
  7.  
  8. //in the search activity
  9. private void search(String str){
  10. ListView listView = (ListView)findViewById(R.id.resultsListView1);
  11. String[] from;
  12. int[] to;
  13. from = new String[]{"_id"};
  14. to = new int[]{R.id.listCellTextView};
  15.  
  16. @SuppressWarnings("deprecation")
  17. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_cell, db.searchStr(str), from, to);
  18. listView.setAdapter(adapter);
  19. listView.setOnItemClickListener(this);
  20. }

Report this snippet  

You need to login to post a comment.