【发布时间】:2015-08-03 09:57:19
【问题描述】:
我在实现 SearchView 时遇到问题,这让我抓狂。 我已经按照这个问题的答案中建议的方法在 ActionBar 中实现了一个 SearchView:
Implementing SearchView in action bar
我已根据自己的需要修改了建议的代码,因此建议会根据输入的内容进行过滤,效果很好,只是我无法让 OnSuggestionListener 工作!
包含 SearchView 操作栏的相关活动部分:
public class MyGroupsActivity extends ActionBarActivity {
private List<String> allItems;
private Menu menu;
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_groups);
allItems = getAllItems();
}
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my_groups, menu);
this.menu = menu;
// Associate searchable configuration with the SearchView
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
System.out.println("onQueryTextSubmit " + s);
return true;
}
@Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void loadHistory(String query) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
String[] columns = new String[] { "_id", "text" };
Object[] temp = new Object[] { 0, "default" };
MatrixCursor cursor = new MatrixCursor(columns);
List<String> result = new ArrayList<>();
for(int i = 0; i < allGroups.size(); i++) {
if(allItems.get(i).toLowerCase().contains(query.toLowerCase())){
temp[0] = i;
temp[1] = allItems.get(i);
cursor.addRow(temp);
result.add(allItems.get(i));
}
}
searchView.setSuggestionsAdapter(new SearchViewAdapter(this, cursor, result));
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
System.out.println("onSuggestionSelect");
Cursor cursor = (Cursor) searchView.getSuggestionsAdapter().getItem(i);
String feedName = cursor.getString(1);
searchView.setQuery(feedName, false);
searchView.clearFocus();
return true;
}
@Override
public boolean onSuggestionClick(int i) {
System.out.println("onSuggestionClick");
Cursor cursor = (Cursor) searchView.getSuggestionsAdapter().getItem(i);
String feedName = cursor.getString(1);
System.out.println(feedName + " clicked ");
searchView.setQuery(feedName, false);
searchView.clearFocus();
return true;
}
});
}
}
}
我的 CustomAdapter 子类:
public class SearchViewAdapter extends CursorAdapter {
private List<String> items;
private TextView text;
public SearchViewAdapter(Context context, Cursor cursor, List<String> items) {
super(context, cursor, false);
this.items = items;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
text.setText(items.get(cursor.getPosition()));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_item, parent, false);
text = (TextView) view.findViewById(R.id.textView);
return view;
}
}
还有我的 search_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:id="@+id/textView"
android:layout_margin="5dp"
android:textColor="@color/accent" />
</RelativeLayout>
我已经尝试过使用 OnClickListener,我尝试将它放在 CustomAdapter 的 bindView 方法中,我尝试在 onCreate 中实例化适配器,还有很多其他的东西,我什至现在都不记得它们了.. 似乎没有工作。当我单击建议列表中的一个项目时,我在 logcat 中得到的只是:
D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
那么,明确一点 - 我如何检测对建议的点击并获取项目的值?
非常感谢您对此的任何帮助和 cmets!谢谢!
【问题讨论】:
标签: android searchview android-cursoradapter