获取建议使用CursorAdapter从ActionBar SearchView单击事件

时间:2015-05-21 15:54:08

标签: android searchview android-cursoradapter

我遇到了一个让我疯狂的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

所以,说清楚 - 如何检测建议点击并获取项目的价值?

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

2 个答案:

答案 0 :(得分:3)

我有一个正常工作的代码,我在设置建议适配器之前设置了onSuggestionListener。你可以尝试一下吗?

答案 1 :(得分:0)

使它起作用的唯一方法是直接在CursorAdapter的视图上绑定自己的onClickListener,而根本不使用onSuggestionListener。