Skip to content Skip to sidebar Skip to footer

Hide Dynamically Added Buttons Based On An If Statement

I can't seem to figure out how to do this, basically I have a list view with a bunch of buttons that each have their own individual values, this all works great until I want to hid

Solution 1:

The problem is with this block of code:

if (battle.equals(false)) {
  battleBtn.setVisibility(View.INVISIBLE);
}
if (ingame.equals(false)) {
  newgameBtn.setVisibility(View.INVISIBLE);
}

As you're aware that ListView items are recycled (that's why you should use a ViewHolder), you also need to set the buttons to View.VISIBLE when the condition is true.

Your code should look like this:

if (battle.equals(false)) {
  battleBtn.setVisibility(View.INVISIBLE);
} else {
  battleBtn.setVisibility(View.VISIBLE);
if (ingame.equals(false)) {
  newgameBtn.setVisibility(View.INVISIBLE);
} else {
  newgameBtn.setVisibility(View.VISIBLE);
}

Solution 2:

I don't know logic of your application but I see error and two possible solution.

  1. Because you looping over victimArray and because NetPlay.myId is in the middle of array you always get battle as false. So you should initiate battle with false and break as soon as it's true.

  2. I assume you should change check:

    NetPlay.victimArray[i].equals(NetPlay.myId)

to

NetPlay.victimArray[i].equals(friend.id)

And similar for another condition.

I would also suggest you to use HashSet instead of primitive victimArray. It will give you more optimal search with contains method instead of looping.

Post a Comment for "Hide Dynamically Added Buttons Based On An If Statement"