Question

So, I have those two lines:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    super.onCreateOptionsMenu(menu);
    getSupportMenuInflater().inflate(R.menu.onime_main, menu);

    // Create the search view
    SearchView searchView = new SearchView(getSupportActionBar()
            .getThemedContext());
    searchView.setQueryHint("Search for anime");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);

    if (mSuggestionsAdapter == null) {
        cursor = new MatrixCursor(COLUMNS);

        cursor.addRow(new String[] { "1", "'Murica" });

        mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
                .getThemedContext(), cursor);
    }

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);

    menu.add("Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_IF_ROOM
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}

And then I have other function:

@Override
public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub

    Log.e("test: ", newText);

    List<Serial> temp = db.getSuggestion(newText);

    MatrixCursor tempCur = new MatrixCursor(COLUMNS);

    for (Serial cn : temp) {
        tempCur.addRow(new String[] { cn.getID() + "", cn.getName() });
        Log.e("Info:", cn.getID() + " " + cn.getName());
    }

    cursor = tempCur;

    for (Serial cn : temp) {
        String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
                + " ,Description: " + cn.getDescription();
        // Writing Contacts to log
        Log.d("Name: ", log);
    }

    mSuggestionsAdapter.notifyDataSetChanged();

    return false;
}

Idea is simple. I have "search box", whatever I type there will be newText. With that will be send sql query, where I get needed data (For autocomplete). Problem is that no matter what method I try, I simply cannot make the 'list' updated. I can add infinity content to just cursor, but then the old ones remains and I don't want that. I dont know anymore how to solve it :(.

If you have any questions, feel free to ask and I'll try to explain or such.

Was it helpful?

Solution

Fixed it. Basically I reset the adapter, setting the new cursor as the main content. I tried that before, but it didnt work, because I didnt make searchView as global (in class). After making it global, I could use searchView and set new adapter, making it work. In other words:

mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar()
            .getThemedContext(), tempCur);

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top