简体   繁体   English

SearchView窗口小部件无法正常工作

[英]SearchView Widget is not working properly

I am trying to filter my listview using searchview acitonbar widget, but when i type something in edittext a black box is appeared on screen. 我正在尝试使用searchview acitonbar小部件过滤列表视图,但是当我在edittext中键入内容时,屏幕上会出现一个黑框。 How to make search without that black box. 如何在没有黑匣子的情况下进行搜索。 在此处输入图片说明

here is my code. 这是我的代码。

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

It is showing that black box because you set the list view to do so: mListView.setTextFilterEnabled(true); 由于您设置了列表视图,因此显示了黑框: mListView.setTextFilterEnabled(true);

EDIT: You said you couldn't make the search without the above line. 编辑:您说没有以上内容,您将无法进行搜索。 It is because you are using text filtering for search purpose. 这是因为您将文本过滤用于搜索目的。 This is not a good way. 这不是一个好方法。 You are calling mListView.setFilterText(query.toString()); 您正在调用mListView.setFilterText(query.toString()); in onQueryTextChange() callback. onQueryTextChange()回调中。 So without enabling the text filtering, your search mechanism does not work. 因此,如果未启用文本过滤功能,则您的搜索机制将无法正常工作。

The proper way is to make your ListView Adapter implement Filterable interface and handle the filtering there. 正确的方法是使ListView适配器实现Filterable接口并在那里处理过滤。

Then you should do the search on the Filter object: Filter.performFiltering(CharSequence) 然后,您应该在Filter对象上进行搜索: Filter.performFiltering(CharSequence)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM