Skip to content

Commit

Permalink
fix: added tag doesn't reflect in adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed May 3, 2021
1 parent 9a55e71 commit 8647bfb
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class TagsArrayAdapter extends RecyclerView.Adapter<TagsArrayAdapter.ViewHolder> implements Filterable {
Expand All @@ -49,14 +50,17 @@ public ViewHolder(CheckedTextView ctv) {
@NonNull
private final TagsList mTags;
/**
* A subset of all tags in {@link #mTags} satisfying the user's search
* A subset of all tags in {@link #mTags} satisfying the user's search.
*
* it will be null if the user search term is empty, in that case
* the adapter should use {@link #mTags} instead to access full list.
*/
@NonNull
@Nullable
private List<String> mFilteredList;

public TagsArrayAdapter(@NonNull TagsList tags) {
mTags = tags;
mFilteredList = tags.copyOfAllTagList();
mFilteredList = null;
sortData();
}

Expand Down Expand Up @@ -86,13 +90,23 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String tag = mFilteredList.get(position);
String tag = getTagByIndex(position);
holder.mTagItemCheckedTextView.setText(tag);
holder.mTagItemCheckedTextView.setChecked(mTags.isChecked(tag));
}

private String getTagByIndex(int index) {
if (mFilteredList == null) {
return mTags.get(index);
}
return mFilteredList.get(index);
}

@Override
public int getItemCount() {
if (mFilteredList == null) {
return mTags.size();
}
return mFilteredList.size();
}

Expand All @@ -106,7 +120,7 @@ private class TagsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint.length() == 0) {
mFilteredList = mTags.copyOfAllTagList();
mFilteredList = null;
} else {
mFilteredList = new ArrayList<>();
final String filterPattern = constraint.toString().toLowerCase(Locale.getDefault()).trim();
Expand Down

0 comments on commit 8647bfb

Please sign in to comment.