Android show SearchView result ListView above ViewPager

Deadly 提交于 2019-11-29 13:11:58

If you want your ListView to show results in real time which user will suggest in searchView - use Filter in your Adapter.

Add to adapter:

ArrayList<full_data> adapterDataList = new ArrayList<full_data>();
ArrayList<filtered_data> mFilteredDataList = new ArrayList<filtered_data>(listOR);
Filter mFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence searchTerm) {
Locale locale = Locale.getDefault();
FilterResults results = new FilterResults();
if ((searchTerm null) || (searchTerm.length() 0)) {
ArrayList<String> searchList = new ArrayList<String>(adapterDataList);
results.values = searchList;
results.count = searchList.size();
} else {
final String searchTermString = searchTerm.toString().toLowerCase(locale);
final String[] words = searchTermString.split(" ");
final int wordCount = words.length;
final ArrayList<String> newValues = new ArrayList<String>();

for (final String value : adapterDataList) {
                    if (value == null) {
                        continue;
                    }
                    final String valueText = value.toLowerCase(locale);
for (int k = 0; k < wordCount; k++) {
                        if (valueText.contains(words[k])) {
                            newValues.add(value);
                            break;
                        }
                    }
                }
                results.values = newValues;
                results.count = newValues.size();
            }
            return results;
        }
@Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mFilteredDataList = (ArrayList&lt;String&gt;)results.values;
            notifyDataSetChanged();
        }
    };
public Filter getFilter(){ return mFilter; }

to SearchView's TextChangedListener:
@Override
public void afterTextChanged(Editable searchText) {
mAdapter.getFilter().filter(searchText);
}

I think it should work fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!