2

I am having a problem with implementing a SearchView that is driving me crazy. I have implemented a SearchView in the ActionBar following the method suggested in the answer for this question:

Implementing SearchView in action bar

I have modified the suggested code to my own needs, so the suggestions get filtered based on what is entered, and it works great, except that I cannot get an OnSuggestionListener to work!

Relevant parts of activity containing the SearchView Actionbar:

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;
                }
            });
        }
    }
}

My CustomAdapter subclass:

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;
    }
}

And my 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>

I have tried with OnClickListener instead, I have tried putting it in the bindView method of the CustomAdapter, I have tried instantiating the adapter in onCreate, and so many other things that I cannot even remember them all now.. Nothing seems to work. All I get in the logcat when I click an item in the suggestion list is:

D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN

So, to put it clear - how do I detect a click on a suggestion and fetch the value of the item?

Any help and comments on this are very much appreciated! Thanks!

Community
  • 1
  • 1
user1419999
  • 156
  • 1
  • 11

2 Answers2

3

I have a working code and I've set up the onSuggestionListener before setting the suggestions adapter. Could you please try the same.

Jayesh Elamgodil
  • 1,467
  • 11
  • 15
0

The only way I made it work is binding own onClickListener directly on view in my CursorAdapter and not using the onSuggestionListener at all.

hrach
  • 2,443
  • 2
  • 26
  • 37