Reputation: 63
The Search function in my Android app works. I'm using onSearchRequested()
; to invoke the Search function. Now what I'd like to do is not use onSearchRequested()
; and pass a string from an EditText
to the search method and display the results in a List. Here's my search as it's working when onSearchRequested
is called:
SearchPage
Activity:
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
Activity:
//--- 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
I'd like to pass a String into the search function String searchData = searchEditText.getText().toString();
and have the search function go to work on the passed string by pressing a "Search" button. Can someone help get me started?
Upvotes: 0
Views: 2485
Reputation: 4183
You should have EditText
like that in your layout:
<EditText
android:id="@+id/search_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="search"
android:imeOptions="actionSearch"
android:inputType="text" />
And in your Activity's onCreate
:
EditText searchQuery = (EditText) findViewById(R.id.search_query);
searchQuery.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String searchData = searchQuery.getText().toString();
showResults(searchData); //passing string to search in your database to your method
return true;
}
return false;
}
});
setOnEditorActionListener
is used to perform search when user presses search button on keayboard. You can read more about imeActions
here.
According to your code call showResults(searchData);
to make search and display results in a list.
EDIT:
According to your code call it in SearchPage
Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView and other your features
DBHelper = new DBAdapter(this);
DBHelper.open();
EditText searchQuery = (EditText) findViewById(R.id.search_query);
Button yourButton = (Button) findViewById(R.id.yourButtonId);
yourButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String searchData = searchQuery.getText().toString();
showResults(searchQuery);
}
});
}
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);
}
Upvotes: 1