С этим ответом я покажу вам, как я выполнял фильтрацию элементов в моем Adapter
.Не отвечая на ваш вопрос напрямую, а скорее предлагая вам другое решение.
Во-первых, у меня есть EditText
в моем ToolBar
, называемом m_app_bar_title_txt
, В моем Activity
я звоню следующее:
//I'm using TextWatcher to see when text changes
m_app_bar_title_txt.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) {
//If the text changes I call the following method
//Passing the text to the metod
filter(s.toString());
}
});
private void filter(String s) {
ArrayList<ScoresData> aData = new ArrayList<>();
for (ScoresData mData : data){
if (mData.textPlayerName.toLowerCase().contains(s.toLowerCase())){
aData.add(mData);
}
}
//this method is in my RecyclerView.Adapter class
//I will provide this below
mAdapter.filterList(aData);
}
mAdapter.filterList(aData);
передает отфильтрованный ArrayList
следующему методу внутри моего RecyclerView.Adapter
:
public void filterList(ArrayList<ScoresData> filteredList){
//Changing the original ArrayList -> data to filteredList
//Then notifying that the list has changed
data = filteredList;
notifyDataSetChanged();
}
Если вам интересно, как выглядит ScoresData
....
public class ScoresData {
public String mImage;
public String textPlayerName;
public String textPos;
public String textTotalScore;
public String textPlayed;
public String textRounds;
}
Надеюсь, это поможет ..