I’m just now figuring out how to implement a Search Dialog in my Android app. I’ve got it working, but it only outputs one column. I think I see how to make it search multiple columns (in my case three columns), but Eclipse says I’m wrong. Here’s my method to get the records in my DBAdapter class:
//--- GET RECORDS FOR SEARCH
public Cursor searchDB(String query) {
String[] allColumns = new String[]{ KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 };
return db.query(true, DB_TABLE, new String[] { KEY_ROWID,
KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 }, KEY_COLUMN1 + " LIKE" + "'%" + query + "%'", null, null, null, null, null);
}
//--- END Get Records for Search
and here’s the method in the activity to display the returned results:
//--- Show Results method
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] from = new String[] { DBAdapter.KEY_COLUMN1 }; //--- change this?
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record, cursor, from, to);
setListAdapter(records);
}
//--- END Show Results method
Right now, the string[] allcolumns isn’t being used. and I assumed that I’d use it by doing
allcolumns + " LIKE"
in the DBAdapter class’ SearchDB() method, and DBAdapter.allcolumns in the showResults method. But allColumns isn’t offered as an option in the showResults() method. What’s the deal? How can I return multiple DB columns with a Search Dialog?
I found the answer in the SearchableDictionary example in the Android SDK. You have to modify your records.xml file so that it has as many textviews as you have columns that you want returned. Also, in the showResults() method, in your from string, add:
DBAdapter.KEY_COLUMN1, DBAdapter.KEY_COLUMN2, DBAdapter.KEY_COLUMN3use your own database call and column names, of course.