3

I have an array of String elements:

suggestion_name = new String[main_data.getJSONArray("cafe").length()];

which is populated by some JSON data:

        for (int i=0; i<suggestion_name.length;i++){

        suggestion_name[i] = main_data.getJSONArray("cafe").getJSONObject(i).getString("name");

        }

When I debug, I see that suggestion_name array has 2 elements in it, everythings fine here. Then I create my suggestions adapter as follows:

public void set_suggestion_adapter_array(){
    String[] from = new String[] {"cafe_name"};
    int[] to = new int[] {R.id.suggestionTextView};
    cursorAdapter = new SimpleCursorAdapter(a,
            R.layout.suggestions_single_item,
            null,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}

and populate the suggestions list like this:

public void populate(String text){
    MatrixCursor matrixCursor = new MatrixCursor(new String[]{ BaseColumns._ID, "cafe_name" });
    for (int i=0; i<suggestion_name.length; i++) {
        if (suggestion_name[i].toLowerCase().startsWith(text.toLowerCase()))
            matrixCursor.addRow(new Object[] {i, suggestion_name[i]});
    }
    cursorAdapter.changeCursor(matrixCursor);
    cursorAdapter.notifyDataSetChanged();
}

I'm calling the populate() method in onQueryTextChange:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // search database for query
            getDataMap.search(query);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // show suggestions
            getDataMap.populate(newText);

            return true;
        }
    });

Yet when I start typing the elements of my suggestion array to the search field, I'm not getting any suggestions. I debugged several times to see if any of my code is not being called in time/at all, yet everything works perfectly in order. My array elements do not contain any odd letters, symbols etc. What am I missing here? Thanks.

Onur Çevik
  • 1,560
  • 13
  • 21

1 Answers1

1

Snap! I forgot to set the adapter.

searchView.setSuggestionsAdapter(cursorAdapter);

Rookie mistake by me. Anyways, maybe someone finds the code and work around useful.

Onur Çevik
  • 1,560
  • 13
  • 21
  • 1
    I read your answer, scoffed, then realised that I too had forgotten to `setSuggestionsAdapter`... – ning Jul 10 '19 at 14:01