code decrease

Asked

Viewed 82 times

2

Is there any other way to accomplish this code?

This code is very repetitive, I wanted to know if it is possible to decrease and make easier the understanding.

if (games.getBallId() == btnBal0l.getId()){
    btnBal0l.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal02.getId()){
    btnBal02.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal03.getId()){
    btnBal03.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal04.getId()){
    btnBal04.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal05.getId()){
    btnBal05.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal06.getId()){
    btnBal06.setBackgroundResource(R.mipmap.ball_verde);
}else if (games.getBallId() == btnBal07.getId()){
    btnBal07.setBackgroundResource(R.mipmap.ball_verde);
}
  • It can be improved with switch { case: }, but it would get bigger

2 answers

4

This is a response based on opinions! There are several ways to perform the same operation. Here I present only one, based on my point of view!

I believe there’s a way to make it more readable.

For this, we can break the functionalities:

  1. Select a Button through a Id
  2. Setar the background in a Button

I suggest you add the 7 buttons to a list right after you create them!

Example:

private List<Button> buttons = new ArrayList<>(0);
buttons.add(btnBal0l);

Instead of keeping a reference in the class, and another in the list, you can create it directly in the list :

buttons.add(Button.class.cast(findViewById(R.id.btnBal01));

So we can get Button through Id from the list, let’s create a method:

private Button getButtonById(int id){
    for(final Button b : buttons){
       if(id == b.getId()){
           return b;
     }
   }
  return null;
}

Now let’s set the resource in the Button:

 private void setImageInButton(int gameId){
        final Button b = getButtonById(gameId);
        if( b != null){
            b.setBackgroundResource(R.mipmap.ball_verde);
        }
    }

Now you can call the function as follows:

setImageInButton( games.getBallId() );

Follow the complete and commented code:

 /**
     * Vamos armazenar em uma lista, todos os botões...
     */
    private List<Button> buttons = new ArrayList<>(0);


    /**
     * verifica se o Button com o mesmo id está na lista
     * O retorna, caso exista, senão retorna null.
     * @param id
     * @return
     */
    private Button getButtonById(int id){
        for(final Button b : buttons){
            if(id == b.getId()){
                return b;
            }
        }
        return null;
    }


    /**
     * Através de um id, seleciona o Button e adiciona a imagem caso não seja nulo!
     * @param gameId
     */
    private void setImageInButton(int gameId){
        final Button b = getButtonById(gameId);
        if( b != null){
            b.setBackgroundResource(R.mipmap.ic_gps_action);
        }
    }

4

If all these buttons are within the same layout, you can use the method findViewById() view class (this layout).

int id = games.getBallId();
Button button = (Button)layout.findViewById(id);
button.setBackgroundResource(R.mipmap.ball_verde);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.