I Wanna Know
I Wanna Know

Reputation: 267

Why changing boolean value inside a class doesn't make effect outside it

I created a class to ensure only two items could be chosen.

All items has a status that can be True or False.

So the method ask three items and three status. I try to make possible only two items to be visible and with status True.

class EscolheItens {

    private void escolheItem(boolean um, boolean dois, boolean tres, ImageView one, ImageView two, ImageView three) {
        if (um == false && dois == true && tres == true) {
            one.setVisibility(View.INVISIBLE);
        } else if ((um == false && dois == true && tres == false) || (um == false && dois == false && tres == true)) {
            one.setVisibility(View.INVISIBLE);
        } else if (um == false && dois == false && tres == false) {
            one.setVisibility(View.INVISIBLE);
            dois = true; //here I put it true 
            two.setVisibility(View.VISIBLE);
        }
    }
}

Now I use the method from this class

 livrofechado.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                livroFechadoStatus = false;
                EscolheItens NovaEscolha = new EscolheItens();
                NovaEscolha.escolheItem(livroFechadoStatus, barrasOuroStatus, antidotoAranhaStatus, livrofechado, barrasOuro, antidotoAranha);
            }
        });

        barrasOuro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                barrasOuroStatus = false;
                EscolheItens NovaEscolha = new EscolheItens();
                NovaEscolha.escolheItem(barrasOuroStatus, antidotoAranhaStatus, livroFechadoStatus, barrasOuro, antidotoAranha, livrofechado);
            }
        });

        antidotoAranha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antidotoAranhaStatus = false;
                EscolheItens NovaEscolha = new EscolheItens();
                NovaEscolha.escolheItem(antidotoAranhaStatus, livroFechadoStatus, barrasOuroStatus, antidotoAranha, livrofechado, barrasOuro);
            }
        });

The problem is all the boolean keep False. I know I set false on all Onclicks but inside method they should change.

I really tried many things before posing here. But nothing worked.

Upvotes: 0

Views: 34

Answers (1)

John Joe
John Joe

Reputation: 12803

If you want to make the change after the boolean value changed, create a method inside

class EscolheItens {
    private void escolheItem(boolean um, boolean dois, boolean tres, ImageView one, ImageView two, ImageView three) {
        if (um == false && dois == true && tres == true) {
            one.setVisibility(View.INVISIBLE);
        } else if ((um == false && dois == true && tres == false) || (um == false && dois == false && tres == true)) {
            one.setVisibility(View.INVISIBLE);
        } else if (um == false && dois == false && tres == false) {
            one.setVisibility(View.INVISIBLE);
            dois = true; //here I put it true 
            two.setVisibility(View.VISIBLE);
            update(um,dois,tres,one);  // call method
        }
    }

    public void update(boolean um, boolean dois, boolean tres, ImageView one) {
        if (um == false && dois == true && tres == true) {
            one.setVisibility(View.INVISIBLE);
        }
    }
}

Upvotes: 1

Related Questions