I need to filter an ArrayList each time that user input a string in a textbox or/and click a button to add boolean attribute to filters.
public class ItemULD {
private int idULD;
private String contour;
private String numULD;
private boolean withNet;
}
And I tried with this method to filter the string in textbox
List<Integer> ids = grupoChip.getCheckedChipIds();
for (Integer id : ids) {
Chip chip = grupoChip.findViewById(id);
ArrayList<ItemULD> arrayFiltered; = new ArrayList<>();
for (ItemULD uld : arrayULD) {
String num = String.valueOf(uld.getNumULD());
if (num.contains(chip.getText()) || uld.getContour().contains(chip.getText())) {
if (!arrayFiltered.contains(uld))
arrayFiltered.add(uld);
}
}
//Method to filter ULD's with net
public static List<ItemULD> filters(List<ItemULD> allULD, boolean net){
List<ItemULD> list = new ArrayList<>();
for (ItemULD uld : allULD) {
if (!net || uld.iswithNet()) {
list.add(uld);
}
}
return list;
}
The problem is when I try to add a new filter on the filtered results that didn't works.. E.G. I have this.
idULD:1,
contour:EK,
numULD: 123456,
withNet:1
idULD:2,
contour:AF,
numULD: 652466,
withNet:0
idULD:3,
contour:AF,
numULD: 123663,
withNet:0
If I put on my seach textbox 123 I see 2 results id1 & id3 this is ok, but when I add AF I see two AF not only the match AF + 123 that would be the id3.
On the other hand, if I click the option to filter by attribute withNet I see all the results. I guess this is caused because I'm creating a new array in the method to return but, how I could do it?
EDIT
I use a ChipGroup to add Chip elements with the string that user write in a textbox. Then I iterate the ChipGroud to get the text of every Chip. So if the user write "123" + Enter a new Chip is added to the group
chip? If yes, it is unclear to me why the code would work for multiple criteria within the same text box (i.e. AF + 123), since you check if the complete text is contained:num.contains(chip.getText()) || uld.getContour().contains(chip.getText())