How to set visibility using multiple cardView ? | Sololearn: Learn to code for FREE!

+1

How to set visibility using multiple cardView ?

I'm trying to hide / show multiple cardView. I used Android Studio and Java language. I need something like this: CardView[ ] cards= { card1, cards, card3 }; for(int I=0;I<3;I++){ cards[I].setVisibility(true); } I can't create an array with CardView. What is the best way to set something using FOR loop ? Leonardo Leal

1/8/2022 12:25:03 AM

Pedro H.J

9 Answers

New Answer

+1

Pedro H.J In this example they used an array list. Maybe the second answer is more interesting. https://stackoverflow.com/questions/40203611/declare-multiple-views-with-a-similar-name-android-studio

+3

Thank you so much Denise Roßberg and KrOW

+1

Your logic is awkward. instead try this: cards[l].setVisibility(true);

+1

I'm sorry. I made a mistake. I tried cards[I] but it does not work. I don't why I can't create a array with cardView.

+1

The cardview's are used with a listview or a recyclerview ?

+1

KrOW ListView adapter.

+1

ListView use reclycing method for performance reason than it dont contains all childs view but only visualized one (more others). At basics, you have to write your adapter such as it update the view visibility (in getView()) reflecting the model (adding to model some property as example boolean is_visible) then notify the adapter with notifyDataSetChanged() when you want change the model attribute (is_visible) for some item. A more robust approch (and without problems) is apply a logic on adapter and rewriting some methods like getCount(), getItem() and getItemId(). At examples getCount() have to return the number of visible items and getItem() have to return the nth visible item.. I know, is not so fast, but recycling view offer perfomance benefits but is more "hard" to manage.

0

KrOW ListView

0

How you add the items? You use an adapter?