Sorting or filtering Recyclerview with Chips in Android

1.1k Views Asked by At

What is the best way to implement a filtering ChipGroup on a recyclerview? I have an EditText that user can put a value for filtering, after press Enter key a chip is added on the ChipGroup. How I can filter all the users put to the group?

Code

 //txtBuscarULD is an Edittext field
 public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        if(txtBuscarULD.getText().length()>0){
            if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n")) {
                chip = new Chip(PlanchasActivity.this);
                chip.setText(txtBuscarULD.getText().toString().replace("\n",""));

                chip.setCloseIconVisible(true);
                chip.setCheckable(true);
                chip.setChecked(true);
                chip.setOnCloseIconClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        grupoChip.removeView(view);
                    }
                });

                grupoChip.addView(chip);
                txtBuscarULD.setText("");
                filterChips();
         }
       }

  private void filterChips() {
    ArrayList<ItemULD> arrayFiltrada = new ArrayList<>();
    List<Integer> ids = grupoChip.getCheckedChipIds();
    List<String> lista = new ArrayList<>();
    for (Integer id:ids){
        Chip chip = grupoChip.findViewById(id);
        lista.add(chip.getText().toString());
    }
    for (ItemULD uld : arrayULD) {
        for (String str : lista) {
            if (uld.getContorno().toUpperCase().contains(str.toUpperCase())
            || uld.getNumULD().toUpperCase().contains(str.toUpperCase())) {
                arrayFiltrada.add(uld);
            }
        }

    }
    adapter.filterList(arrayFiltrada);
    recyclerULD.setAdapter(adapter);
}

This is not working as I expect, (image) (ignore "Red"):

[Image of app]

This repeat the "EK" item cause it has the 2 conditions. And the item "AF" should not appear cause is not like "EK"

0

There are 0 best solutions below