I have a problem with my SearchView
Suggestion, when I write one character in the SearchView
the Suggestion doesn't come.
But when I write two characters in the EditText
of my SearchView
the Suggestion will come.
So I want that my Suggestion already come when I write one character in the EditText
of my SearchView
, what can I do?
My Code from MainActivity:
private SearchView mSearchView;
private SimpleCursorAdapter mAdapter;
private ArrayList<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = new ArrayList<String>();
mList.add("Test 1");
mList.add("Test 2");
mList.add("Test 3");
mList.add("Test 4");
mList.add("Test 5");
mList.add("Test 6");
mList.add("Test 7");
mList.add("Test 8");
mList.add("Test 9");
mList.add("Test 10");
ListView listView = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
listView.setAdapter(adapter);
mAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.suggestion, null, new String[] {""}, new int[] {R.id.suggestion_textView}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setSuggestionsAdapter(mAdapter);
// onItemClick
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener()
{
@Override
public boolean onSuggestionSelect(int position)
{
return false;
}
@Override
public boolean onSuggestionClick(int position)
{
// Only for Test
finish();
return false;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String s)
{
return false;
}
@Override
public boolean onQueryTextChange(String s)
{
final MatrixCursor c = new MatrixCursor(new String[] { BaseColumns._ID, "" });
for (int i=0; i < mList.size(); i++)
{
c.addRow(new Object[] {i, mList.get(i)});
}
mAdapter.changeCursor(c);
return false;
}
});
return true;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…