SearchView小部件无法正常工作

时间:2014-06-15 18:56:49

标签: android listview android-widget searchview

我正在尝试使用searchview acitonbar小部件过滤我的列表视图,但是当我在edittext中输入内容时,屏幕上会出现一个黑框。如何在没有黑盒的情况下进行搜索。 enter image description here

这是我的代码。

public class MainActivity extends ListActivity implements OnQueryTextListener{

    private Button favListBtn;
    private ListView mListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] categories =  {"Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture"};

        mListView = (ListView) findViewById(android.R.id.list);
        mListView = getListView();

        ArrayAdapter<String> mAdapte = new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1, categories);
        mListView.setAdapter(mAdapte);
        mListView.setTextFilterEnabled(true);

        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {

                      new SearchAsynTask(MainActivity.this).execute();
                      // selected item 
                      String category = ((TextView) view).getText().toString();
                      Intent intent = new Intent(MainActivity.this, BusinessListActivity.class);
                      startActivity(intent);
                      Toast.makeText(MainActivity.this, category, Toast.LENGTH_LONG).show();
                      }
            });       
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {        

        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        if (TextUtils.isEmpty(query))
          {
                mListView.clearTextFilter();
            }
          else
          {
                mListView.setFilterText(query.toString());
            }
        return true;
    }
    }

1 个答案:

答案 0 :(得分:0)

显示黑框,因为您设置了列表视图:mListView.setTextFilterEnabled(true);

修改 你说你没有上面的那条线就无法进行搜索。这是因为您使用文本过滤进行搜索。这不是一个好方法。您在mListView.setFilterText(query.toString());回调中呼叫onQueryTextChange()。因此,如果不启用文本过滤,则搜索机制不起作用。

正确的方法是使ListView Adapter实现Filterable接口并在那里处理过滤。

然后你应该对Filter对象进行搜索:Filter.performFiltering(CharSequence)