所以,我有这两行:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.onime_main, menu);
// Create the search view
SearchView searchView = new SearchView(getSupportActionBar()
.getThemedContext());
searchView.setQueryHint("Search for anime");
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
if (mSuggestionsAdapter == null) {
cursor = new MatrixCursor(COLUMNS);
cursor.addRow(new String[] { "1", "'Murica" });
mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
.getThemedContext(), cursor);
}
searchView.setSuggestionsAdapter(mSuggestionsAdapter);
menu.add("Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
然后我还有其他功能:
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
Log.e("test: ", newText);
List<Serial> temp = db.getSuggestion(newText);
MatrixCursor tempCur = new MatrixCursor(COLUMNS);
for (Serial cn : temp) {
tempCur.addRow(new String[] { cn.getID() + "", cn.getName() });
Log.e("Info:", cn.getID() + " " + cn.getName());
}
cursor = tempCur;
for (Serial cn : temp) {
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
+ " ,Description: " + cn.getDescription();
// Writing Contacts to log
Log.d("Name: ", log);
}
mSuggestionsAdapter.notifyDataSetChanged();
return false;
}
想法很简单。我有“搜索框”,无论我输入什么,都会有newText。用它将发送sql查询,在那里我得到所需的数据(用于自动完成)。问题是无论我尝试什么方法,我都无法更新“列表”。我可以添加无限内容到光标,但旧的仍然保留,我不希望这样。我不知道如何解决它:(。
如果您有任何问题,请随时提出,我会尝试解释或等等。
答案 0 :(得分:1)
修正了它。基本上我重置了适配器,将新光标设置为主要内容。我之前尝试过,但它没有用,因为我没有将searchView视为全局(在课堂上)。在使其成为全局之后,我可以使用searchView并设置新的适配器,使其工作。换句话说:
mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
.getThemedContext(), tempCur);
searchView.setSuggestionsAdapter(mSuggestionsAdapter);