android – SearchView建议不适用于一个字符

前端之家收集整理的这篇文章主要介绍了android – SearchView建议不适用于一个字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的SearchView建议有问题,当我在SearchView中写一个字符时,建议没有到来.
但是当我在SearchView的EditText中写两个字符时,建议就会到来.

所以当我在SearchView的EditText中写一个字符时,我希望我的建议已经来了,我该怎么办?

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

解决方法

我自己找到了解决方案.它不干净但它会起作用.
我调试了SearchView,然后我从SearchView找到了AutoCompleteTextView的ID.比我将AutoCompleteTextView的阈值从2设置为1.现在它可以工作.
@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);
    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;
}
原文链接:https://www.f2er.com/android/318494.html

猜你在找的Android相关文章