How to set visibility using multiple cardView ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

8th Jan 2022, 12:25 AM
Pedro H.J
Pedro H.J - avatar
9 Answers
+ 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
9th Jan 2022, 11:51 AM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Thank you so much Denise Roßberg and KrOW
9th Jan 2022, 1:14 PM
Pedro H.J
Pedro H.J - avatar
+ 1
Your logic is awkward. instead try this: cards[l].setVisibility(true);
8th Jan 2022, 2:49 AM
Shadoff
Shadoff - avatar
+ 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.
8th Jan 2022, 3:10 AM
Pedro H.J
Pedro H.J - avatar
+ 1
The cardview's are used with a listview or a recyclerview ?
8th Jan 2022, 2:33 PM
KrOW
KrOW - avatar
+ 1
KrOW ListView adapter.
9th Jan 2022, 12:20 AM
Pedro H.J
Pedro H.J - avatar
+ 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.
9th Jan 2022, 12:14 PM
KrOW
KrOW - avatar
0
KrOW ListView
8th Jan 2022, 7:59 PM
Pedro H.J
Pedro H.J - avatar
0
How you add the items? You use an adapter?
8th Jan 2022, 8:09 PM
KrOW
KrOW - avatar