Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android - SearchView Suggestion doesn't work with one character

I have a problem with my SearchView Suggestion, when I write one character in the SearchView the Suggestion doesn't come. But when I write two characters in the EditText of my SearchView the Suggestion will come.

So I want that my Suggestion already come when I write one character in the EditText of my SearchView, what can I do?

My Code from MainActivity:

private SearchView mSearchView;
private SimpleCursorAdapter mAdapter;
private ArrayList<String> mList;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mList = new ArrayList<String>();
    mList.add("Test 1");
    mList.add("Test 2");
    mList.add("Test 3");
    mList.add("Test 4");
    mList.add("Test 5");
    mList.add("Test 6");
    mList.add("Test 7");
    mList.add("Test 8");
    mList.add("Test 9");
    mList.add("Test 10");

    ListView listView = (ListView) findViewById(android.R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList);
    listView.setAdapter(adapter);

    mAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.suggestion, null, new String[] {""}, new int[] {R.id.suggestion_textView}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    mSearchView = (SearchView) searchItem.getActionView();

    mSearchView.setSuggestionsAdapter(mAdapter);

    // onItemClick
    mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener()
    {
        @Override
        public boolean onSuggestionSelect(int position)
        {
            return false;
        }

        @Override
        public boolean onSuggestionClick(int position)
        {
            // Only for Test
            finish();
            return false;
        }
    });

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String s)
        {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s)
        {
            final MatrixCursor c = new MatrixCursor(new String[] { BaseColumns._ID, "" });

            for (int i=0; i < mList.size(); i++)
            {
                    c.addRow(new Object[] {i, mList.get(i)});

            }
            mAdapter.changeCursor(c);
            return false;
        }
    });

    return true;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a solution myself. It's not clean but it will work. I have debug into SearchView and then I found the ID of the AutoCompleteTextView from SearchView. Than I set the Threshold for the AutoCompleteTextView from 2 to 1. Now it works.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    mSearchView = (SearchView) searchItem.getActionView();

    // Solution
    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null);
    AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(autoCompleteTextViewID);
    searchAutoCompleteTextView.setThreshold(1);
    ///////////

    mSearchView.setSuggestionsAdapter(mAdapter);

    // onItemClick
    mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener()
    {
        @Override
        public boolean onSuggestionSelect(int position)
        {
            return false;
        }

        @Override
        public boolean onSuggestionClick(int position)
        {
            // Only for Test
            finish();
            return false;
        }
    });

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String s)
        {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s)
        {
            final MatrixCursor c = new MatrixCursor(new String[] { BaseColumns._ID, ""     });
            for (int i=0; i < mList.size(); i++)
            {
                if (mList.get(i).toLowerCase().startsWith(s.toLowerCase()))
                {
                    c.addRow(new Object[]{i, mList.get(i)});
                }
            }
            mAdapter.changeCursor(c);

            return false;
        }
    });
return true;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...