需要基于EditText
对我的RecyclerView
实现搜索功能。键入时必须过滤列表。我有一个edittext
和一个Rexyclerview
,我想用Recyclerview
过滤edittext
。我在Google上进行了大量搜索,发现了一些示例,但没有一个对我有用,这是我的代码。我需要先清除列表,然后再添加结果
My search feature is filtering but showing same data
public void filter(String s) {
String text;
text = s.toString().toLo`enter code here`werCase();
text = text.toLowerCase().trim();
// text = s.toLowerCase(Locale.getDefault());
mDisplayedValues = new ArrayList<SelectedAthleteDEtail>();
mDisplayedValues.clear();
if (text.length() == 0) {
mDisplayedValues = AthleteData;
// mDisplayedValues.addAll(AthleteData);
BoardRecyclerData.setAdapter(adapter);
notifyDataSetChanged();
} else {
mDisplayedValues.clear();
notifyDataSetChanged();
for (SelectedAthleteDEtail cs : AthleteData) {
if (cs.getFirstName().toLowerCase().contains(text)) {
mDisplayedValues.add(cs);
Log.d(TAG, "filterab: "+cs.getFirstName());
Log.d(TAG, "filterxyz: "+mDisplayedValues);
}
}
}
BoardRecyclerData.setAdapter(adapter);
//notifyDataSetChanged();
}
}
答案 0 :(得分:0)
对于您的editText:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String userSearch = search.trim().toLowerCase();
List<Model> filteredData = new ArrayList<>();
if (!TextUtils.isEmpty(userSearch)) {
for (Model data : mainDataList) {
if (data.getSomething().toLowerCase().contains(userSearch.toLowerCase())) {
filteredData.add(data);
}
}
if (filteredData.size() == 0) {
//Your Action
} else {
//Your Action
}
usersAdapter.filterAdapter(filteredData);
} else {
usersAdapter.filterAdapter(usersList);
}
}
在适配器中:
public void filterAdapter(List<Model> filteredData) {
usersList = new ArrayList<>();
usersList.addAll(filteredData);
notifyDataSetChanged();
}
答案 1 :(得分:0)
我认为SearchView是您所需要的。
There很不错。