我的 Android 应用程序中的搜索功能有效。我正在使用onSearchRequested()
;调用搜索功能。现在我想做的是不使用onSearchRequested()
;并将字符串从 an 传递EditText
给 search 方法并在 List 中显示结果。这是我的搜索,因为它在onSearchRequested
被调用时起作用:
SearchPage
活动:
DBHelper = new DBAdapter(this);
DBHelper.open();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
//--- END onSearchRequested
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_YEAR,
DBAdapter.KEY_MAKE, DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rYearTV, R.id.rMakeTV,
R.id.rModelTV };
final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBAdapter
活动:
//--- GET RECORDS FOR SEARCH
public Cursor searchDB(String query) {
String[] parts = query.split(" ");
String queryString = "";
for(int i = 0; i < parts.length; i++) {
queryString += KEY_YEAR + " LIKE '%" + parts[i] + "%' OR ";
queryString += KEY_MAKE + " LIKE '%" + parts[i] + "%' OR ";
queryString += KEY_MODEL + " LIKE '%" + parts[i] + "%'";
if(i != (parts.length - 1)) {
queryString += " OR ";
}
}
return db.query(true, DB_TABLE,
new String[] { KEY_ROWID, KEY_SDATE, KEY_YEAR, KEY_MAKE, KEY_MODEL },
queryString, null, null, null, null, null);
}
//--- END Get Records for Search
我想将一个字符串传递给搜索函数String searchData = searchEditText.getText().toString();
,并让搜索函数通过按“搜索”按钮来处理传递的字符串。有人可以帮助我开始吗?