Reputation: 863
I have just implemented the "android.support.v7.widget.SearchView" in my Actionbar to search for some pdf files. When i insert multiple characters for search, the list of suggestions is shown perferctly. But when i'm only entering one single character, there are no proposals shown, although the function to populate the suggestions is called. Do you have an idea how to change that behavior, to get the proposals shown when entering only one character?
This is where i simply define the searchview inside a menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/pdf_menu_search_item"
android:icon="@mipmap/ic_search_white_24dp"
android:title="Suche"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/></menu>
In my Activity, the following code is implemented:
private static final String[] SUGGESTIONS = {
"Amsterdam","Berlin","Rome"
};
private SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_contextual_actionbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final SearchView mSearchView;
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));
final String[] from = new String[] {"cityName"};
final int[] to = new int[] {android.R.id.text1};
mAdapter = new SimpleCursorAdapter(Startseite.this,
android.R.layout.simple_list_item_1,
null,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mSearchView.setSuggestionsAdapter(mAdapter);
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
return true;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
populateAdapter(newText);
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
// You must implements your logic to get data using OrmLite
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{ BaseColumns._ID, "cityName" });
for (int i=0; i<SUGGESTIONS.length; i++) {
if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase()))
{ c.addRow(new Object[] {i, SUGGESTIONS[i]}); }
}
mAdapter.changeCursor(c);
mAdapter.notifyDataSetChanged();
}
Working with more than one characters: Not working with only one character:
Please has anyone an idea how to fix that?
Upvotes: 2
Views: 691
Reputation: 863
I have just found the answer. If you are using AppCompatActivity (like me), you can set the searchview threshold like that:
AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(getResources().getIdentifier("search_src_text", "id", getPackageName()));
searchAutoCompleteTextView.setThreshold(1);
Now the search-suggestions will be shown from the first character on. Maybe it is helpful for others :)
Upvotes: 2