Skip to content

Commit

Permalink
Fixed bug NullPointerException in Comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
pchmn committed May 4, 2017
1 parent f447222 commit 1d85b59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
Binary file added docs/material-chips-input-sample-v1.0.4_4.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class FilterableAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
private ColorStateList mTextColor;
// recycler
private RecyclerView mRecyclerView;
// sort
private Comparator<ChipInterface> mComparator;
private Collator mCollator;


public FilterableAdapter(Context context,
Expand All @@ -59,23 +62,18 @@ public FilterableAdapter(Context context,
ColorStateList textColor) {
mContext = context;
mRecyclerView = recyclerView;
if(chipList == null) {
Log.e(TAG, "The filterable list is null");
Toast.makeText(context, "The filterable list is null", Toast.LENGTH_LONG).show();
}
else {
Collections.sort(chipList, new Comparator<ChipInterface>() {
@Override
public int compare(ChipInterface o1, ChipInterface o2) {
Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.PRIMARY);
return collator.compare(o1.getLabel(), o2.getLabel());
}
});
mOriginalList.addAll(chipList);
mChipList.addAll(chipList);
mFilteredList.addAll(chipList);
}
mCollator = Collator.getInstance(Locale.getDefault());
mCollator.setStrength(Collator.PRIMARY);
mComparator = new Comparator<ChipInterface>() {
@Override
public int compare(ChipInterface o1, ChipInterface o2) {
return mCollator.compare(o1.getLabel(), o2.getLabel());
}
};
sortList(chipList);
mOriginalList.addAll(chipList);
mChipList.addAll(chipList);
mFilteredList.addAll(chipList);
mLetterTileProvider = new LetterTileProvider(mContext);
mBackgroundColor = backgroundColor;
mTextColor = textColor;
Expand Down Expand Up @@ -248,23 +246,9 @@ private void addChip(ChipInterface chip) {
mChipList.add(chip);
mFilteredList.add(chip);
// sort original list
Collections.sort(mChipList, new Comparator<ChipInterface>() {
@Override
public int compare(ChipInterface o1, ChipInterface o2) {
Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.PRIMARY);
return collator.compare(o1.getLabel(), o2.getLabel());
}
});
sortList(mChipList);
// sort filtered list
Collections.sort(mFilteredList, new Comparator<ChipInterface>() {
@Override
public int compare(ChipInterface o1, ChipInterface o2) {
Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.PRIMARY);
return collator.compare(o1.getLabel(), o2.getLabel());
}
});
sortList(mFilteredList);

notifyDataSetChanged();
}
Expand All @@ -278,5 +262,7 @@ private boolean contains(ChipInterface chip) {
return false;
}


private void sortList(List<? extends ChipInterface> list) {
Collections.sort(list, mComparator);
}
}

0 comments on commit 1d85b59

Please sign in to comment.