我有一个recyclerview,其中显示了单词列表,并且在右侧,每个视图都有书签按钮。一切正常,但是如果已经有加书签的单词并且如果我过滤rycyclerview,则书签的图像不会更改。您可以在以下屏幕截图中看到它们:
在过滤所有内容之前,您可以看到:标记了3个单词
但是当我过滤回收站视图时,实际上在该位置上还标记了另外3个单词
这是我的代码,用于获取书签单词并与其他人进行比较并标记它们是否匹配:
光标c = database.rawQuery(“从书签中选择标题,在标题中输入(从单词中选择标题)”,null);
while (c.moveToNext()){
String title = c.getString(0);
if (title.equals(holder.wordsName.getText().toString())){
holder.wordsFav.setImageResource(R.drawable.ic_bookmark_black_24dp);
holder.wordsFav.setTag("clicked");
}
else if (!title.equals(holder.wordsName.getText().toString())){
holder.wordsFav.setImageResource(R.drawable.ic_bookmark_border_black_24dp);
}
}
这是过滤代码: 在适配器中:
@Override
public Filter getFilter() {
return wordsFilter;
}
private Filter wordsFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Words> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(wordsListFull);
}else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Words words: wordsListFull){
if (words.getWord().toLowerCase().contains(filterPattern)){
filteredList.add(words);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
wordsList = new ArrayList<Words>();
wordsList.clear();
if (wordsList != null){
wordsList.addAll((List) results.values);
}
notifyDataSetChanged();
}
};
并且在片段中:
searchWords.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) {
if (s.length() != 0) {
clearTextBtn.setVisibility(View.VISIBLE);
clearTextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchWords.setText("");
clearTextBtn.setVisibility(View.GONE);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
} else {
clearTextBtn.setVisibility(View.GONE);
}
wordsAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 0 :(得分:0)
如果该特定标题不在书签中,请尝试将图像重置为灰色。
Cursor c = database.rawQuery("SELECT title FROM bookmark WHERE title IN (SELECT title FROM words)", null);
while (c.moveToNext()){
String title = c.getString(0);
if (title.equals(holder.wordsName.getText().toString())){
holder.wordsFav.setImageResource(R.drawable.ic_bookmark_black_24dp);
holder.wordsFav.setTag("clicked");
}else{
holder.wordsFav.setImageResource(R.drawable.default_image);
}
}