gpt4 book ai didi

android - 使用 CursorAdapter 从 ActionBar SearchView 获取建议点击事件

转载 作者:行者123 更新时间:2023-11-29 17:36:20 25 4
gpt4 key购买 nike

我在实现 SearchView 时遇到问题,这让我发疯。我按照这个问题的答案中建议的方法在 ActionBar 中实现了一个 SearchView:

Implementing SearchView in action bar

我已经根据自己的需要修改了建议的代码,所以建议会根据输入的内容进行过滤,而且效果很好,只是我无法让 OnSuggestionListener 工作!

包含 SearchView Actionbar 的 Activity 的相关部分:

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

那么,说清楚一点——我如何检测对建议的点击并获取该项目的值?

非常感谢对此的任何帮助和评论!谢谢!

最佳答案

我有一个工作代码,并且在设置建议适配器之前设置了 onSuggestionListener。你能试试同样的吗?

关于android - 使用 CursorAdapter 从 ActionBar SearchView 获取建议点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30378712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com