我只想在'searchview'元素中的书写字符多于3时打印研究结果。 通过这种方式,当我有3个书写字符时,我可以获取数据,但只有当我写了另外1个字符时,我才能看到结果。 当书面字符为4而不是5时,我需要查看结果。 当字符为4时,如何查看结果?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
final String[] from = new String[] {"title"};
final int[] to = new int[] {android.R.id.text1};
myAdapter = new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.search_view);
final SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setIconified(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if(s.length() > 3) {
new Data().execute(s);
}
MatrixCursor mc = new MatrixCursor(new String[]{ BaseColumns._ID, "Title" });
for (int i=0; i<strArrData.length; i++) {
if (strArrData[i].toLowerCase().startsWith(s.toLowerCase()))
mc.addRow(new Object[] {i, strArrData[i]});
}
myAdapter.changeCursor(mc);
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
searchView.setSuggestionsAdapter(myAdapter);
return false;
}
});
}
return true;
}
编辑:
myAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
MatrixCursor mc = new MatrixCursor(new String[]{BaseColumns._ID, "Title"});
for (int i = 0; i < strArrData.length; i++) {
mc.addRow(new Object[]{i, strArrData[i]});
}
return mc;
}
});
searchView.setSuggestionsAdapter(myAdapter);