3

I have Model Class Like this:

private String animalName, type;

public Model(){}

public Model(String animalName, String type){
 this.animalName = animalName;
 this.type = type;
}
//and getter and setters for all vars

And ArrayList<Model>(); according to my model class

so I want to check if my ArrayList<>() contains a specific Type ie. herbivore then add those animals according to their type in different ArrayList and I want to make CHOICE CHIPS according to my type. here is an example of my app:

enter image description here

Note I am using RecyclerView to display Images

How Can I add That Choice Chips According to my Model Class and OnClick on any chips load shows Images according to that.

Any Solution is Appreciated.

James Bond
  • 47
  • 12
  • 1
    Does this answer your question? [Dynamically add chips to chipgroup](https://stackoverflow.com/questions/61847481/dynamically-add-chips-to-chipgroup) – MRamzan Sep 12 '21 at 08:47
  • https://stackoverflow.com/a/61847604/9411376 see this may solve your issue – MRamzan Sep 12 '21 at 08:48

1 Answers1

2

You can Create A Method name it like this or whatever you want

private void setChips(String type){
    Chip chip = new Chip(this);
    chip.setText(chipsTitle);
    chip.setCheckable(true);
    //if you not create chipGroup in you xml yet then create it 
    chipGroup.addView(chip);
}

And then you can call that method and pass your type for ie - "Herbivores" etc.

setChips("Herbivores");

And After that you can add ClickListeners to Your chips like this:

for (int i = 0; i < chipGroup.getChildCount(); i++) {
     int finalI = i;
     chipGroup.getChildAt(i).setOnClickListener(v -> {
            Chip chip = chipGroup.findViewById(chipGroupFl.getChildAt(finalI).getId());
            chip.setChecked(true);

            //here you can call your method to load Images
            

        });
    }
Vishal Beep
  • 1,873
  • 1
  • 10
  • 25