简体   繁体   中英

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. 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).

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. 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.

How can I do this? I already have my SearchableActivity and configuration from following the guide.

You would be able yo search your listview with this. Here I am using editte1xtSearch where your will add the text to be searched.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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