简体   繁体   English

Android SearchView小部件不在操作栏中

[英]Android SearchView widget NOT in actionbar

I'd like to add a searchView in my layout and place it where I want, unfortunately all I could find how to do was to add it to the actionbar. 我想在自己的布局中添加一个searchView并将其放置在我想要的位置,不幸的是,我能找到的所有方法是将其添加到动作栏中。 I tried just dragging the searchView widget into an activity's layout, but wasn't sure where to go from there (It popped up a huge list of null pointer exceptions on the design). 我尝试仅将searchView小部件拖到活动的布局中,但不确定从那里开始(它在设计中弹出了大量的空指针异常列表)。

I have followed this guide: http://developer.android.com/guide/topics/search/search-dialog.html and successfully created a searchView in the action bar, which is great for that SearchableActivity. 我遵循了该指南: http : //developer.android.com/guide/topics/search/search-dialog.html并成功在操作栏中创建了searchView,这对于该SearchableActivity非常有用 Now I want to go into another one of my activities, say MainActivity, and add a searchView widget to it and I don't want to put it into the actionbar. 现在,我想进入另一项活动,例如MainActivity,并向其中添加一个searchView小部件,而我不想将其放入操作栏中。

How can I do this? 我怎样才能做到这一点? I already have my SearchableActivity and configuration from following the guide. 按照指南,我已经有了SearchableActivity和配置。

You would be able yo search your listview with this. 您将可以使用此搜索列表视图。 Here I am using editte1xtSearch where your will add the text to be searched. 在这里,我使用editte1xtSearch,您将在其中添加要搜索的文本。

  editTextSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                                      int arg3) {

                String text = edSearch.getText().toString();
                NameListAdapter.filter(text);
            }
                @Override
                public void beforeTextChanged(CharSequence cs, int arg1, int arg2,
                                              int arg3) {
                }
                @Override
                public void afterTextChanged(Editable cs) {
                }
            });

Add the following filter method: 添加以下过滤方法:

// Filter method for search
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    NameArrayList.clear();
    int j;
    if (charText.length() == 0) {
        NameArrayList.addAll(arrayList);
    }
    else
    {
        NameArrayList.clear();
        for (j=0;j<arrayList.size();j++)
        {

       if(arrayList.get(j).toLowerCase(Locale.getDefault()).contains(charText))
            {
                NameArrayList.add((arrayList.get(j)));
            }
        }
    }
    notifyDataSetChanged();
}

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

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