Android – displays suggestions for an empty searchview

There is a searchview widget in my action bar. When the user enters one or more characters, it will display the correct suggested search criteria

I want to display a (different) list of suggestions when searchview is empty, including before the user enters any text

So far, when the user enters text and then deletes the text, it works normally. When the user clicks the search icon to open the widget, once it opens, click the search widget a second time. For this purpose, I use onquerytextlistener, as shown below:

class SearchTextEnteredListener implements SearchView.OnQueryTextListener {

    @Override
    public boolean onQueryTextChange(String newText) {
        if (newText.length() > 1) {
            // Set normal suggestions
            CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext, suggestions);
            mSearchView.setSuggestionsAdapter(searchadapter);
        } else if (newText.length() == 1) {
            // Clear the displayed suggestions
            if(searchadapter != null && mSearchView != null){
                searchadapter.createAndChangeCursor(null);
            }
        } else {
            // Length = 0, show other suggestions
            CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext, otherSuggestions);
            mSearchView.setSuggestionsAdapter(searchadapter);
        }

        return false;
    }
}

However, I hope that once the search widget is opened, it will pop up suggestions, rather than requiring a second click as now. I'm not lucky to do so

I even tried to manually find the AutoCompleteTextView of searchview and call showdropdown() when searchview gets focus (see below), but there is no drop-down list

mSearchView.setOnQueryTextFocuschangelistener(new OnFocuschangelistener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            int autoCompleteId = mContext.getResources().getIdentifier("android:id/search_src_text", null, null);
            if (autoCompleteId == 0) {
                // We won't be able to find the view. Give up.
                return;
            }

            AutoCompleteTextView autoCompleteView = (AutoCompleteTextView) mSearchView.findViewById(autoCompleteId);
            if (autoCompleteView != null) {
                autoCompleteView.showDropDown();
            }
        }
    }

});

How do I create a drop-down list of search suggestions when I open the search widget?

resolvent:

It's possible. I'm using compat V7 searchview and cursor loaders to query the activity lifecycle

// e.g in onCreateView in my Fragment, wire up the SearchView:
MenuItem searchMenuItem = toolbar.getMenu().findItem(R.id.search_menu_item);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
// and setup listeners (implementation is shown later)
searchView.setOnSuggestionListener(this);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
// then raid the SearchView so search begins on 0 chars!
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
autoCompleteTextView.setThreshold(0);
// and set the suggestions adapter so we can use a Loader to do the queries
SuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(getContext(), null, 0);
searchView.setSuggestionsAdapter(suggestionAdapter);

The following is the implementation of the listener:

@Override
public boolean onQueryTextChange(String userInput) {
    if (TextUtils.isEmpty(userInput.trim())) {
        Log.d(TAG, "onQueryTextChange: empty userInput");
        // The argument to the loader is null so your query should return all suggestions
        getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID, null, this);
        return true;
    }

    userInput = userInput.trim();
    Bundle args = new Bundle();
    args.putString("USER_INPUT", userInput);
    // The argument contains the users input so your query should use a LIKE to find matching suggestions
    getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID, args, this);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return true;
}

@Override
public boolean onClose() {
    return false;
}

@Override
public boolean onSuggestionSelect(int position) {
    return false;
}

@Override
public boolean onSuggestionClick(int position) {
    Cursor c = searchView.getSuggestionsAdapter().getCursor();
    final String suggestion = c.getString(c.getColumnIndex("some_column_name"));
    Log.d(TAG, "onSuggestionClick: position: " + position + " suggestion: " + suggestion);
    return true;
}

As another detail, my suggestioncursoradapter extends the cursoradapter and overrides the following methods to prevent: java.lang.illegalstateexception: trying to reopen closed objects: sqlitequery

@Override
public void changeCursor(Cursor newCursor) {
    newCursor.close();
}

Exercise is painful, so I hope it can help others!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>