Android how to optimize a large string of ifs

1

I would like to know how to optimize a large string of ifs so that the code is more efficient and readable.

My Android application uses version 2.3.3 (API 10), in this application I have an activity responsible for displaying user account information, where it can edit this information and update. This activity contains 8 fields next to the field has 1 "Edit" button and a hidden "OK" button, if the "Edit" button is clicked then the application hides the "Edit" and displays the "OK" in the same place. That way I have the total 16 buttons, 8 "Edit" and 8 "OK". That left my onClick method (View v) with 16 ifs.

How can I improve this and make it more efficient and more readable? I know I can use the switch that would be almost the same thing.

The code below has the method with only 2 of the 16 buttons to simplify, since the other buttons follow the same logic.

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId() == R.id.btneditar1) {
        camponome.setSelectAllOnFocus(true);
        camponome.setFocusable(true);
        camponome.setFocusableInTouchMode(true);            
        camponome.requestFocus();
        imm.showSoftInput(camponome, InputMethodManager.SHOW_FORCED);
        btneditar1.setVisibility(View.GONE);
        btnok1.setVisibility(View.VISIBLE);
    }

    if(v.getId() == R.id.btnok1) {
        CampoValido = ValidaCampo.validateNotNull(camponome, "Campos sem dados são inválidos!");
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(camponome, bnome);
            if(CampoValido) {
                final String nome = camponome.getText().toString().trim();
                atualizar.execute(usuario, nome, null, null, null, null, null, null);
                txtnomebd.setText(nome);
            }
            camponome.setFocusable(false);
            camponome.setFocusableInTouchMode(false);           
            camponome.requestFocus();
            btnok1.setVisibility(View.GONE);
            btneditar1.setVisibility(View.VISIBLE);
        }           
    }

    if(v.getId() == R.id.btneditar2) {
        camposobrenome.setSelectAllOnFocus(true);
        camposobrenome.setFocusable(true);
        camposobrenome.setFocusableInTouchMode(true);           
        camposobrenome.requestFocus();
        imm.showSoftInput(camposobrenome, InputMethodManager.SHOW_FORCED);
        btneditar2.setVisibility(View.GONE);
        btnok2.setVisibility(View.VISIBLE);
    }

    if(v.getId() == R.id.btnok2) {
        CampoValido = ValidaCampo.validateNotNull(camposobrenome, "Campos sem dados são inválidos!");
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(camposobrenome, bsobrenome);
            if(CampoValido) {
                final String sobrenome = camposobrenome.getText().toString().trim();
                atualizar.execute(usuario, null, sobrenome, null, null, null, null, null);
                txtsobrenomebd.setText(sobrenome);
            }
            camposobrenome.setFocusable(false);
            camposobrenome.setFocusableInTouchMode(false);          
            camposobrenome.requestFocus();
            btnok2.setVisibility(View.GONE);
            btneditar2.setVisibility(View.VISIBLE);
        }           
    }           

}
    
asked by anonymous 20.01.2016 / 17:25

1 answer

1

As you reflect on the comments and do some research. For this specific case, the only way to optimize ifs would be to place a Listener on each button in the onCreate () method, thus:

findViewById(id).setOnClickListener(new View.OnClickListener() { ... seu código ...} );

However when doing this I overload the onCreate () method, that is, when optimizing one thing I overload another. I do not know if that would be better, but with the help of the comments I came to the conclusion that the ifs in this case were not the problem but the fact of repeating functions. I was worried about ifs and forgot to use object orientation. So the only optimization made was to make a single method for the action of all the buttons that use this method, passing as a parameter what differentiates in the action of each button it looks like this:

public void onClick(View v) {
    // TODO Auto-generated method stub
    ControlaBanco bd = new ControlaBanco(getBaseContext());
    Cursor dados = bd.pegaUsuario();
    bnome = dados.getString(2);
    bsobrenome = dados.getString(3);
    busuario= dados.getString(4);
    bpaisregiao = dados.getString(6);
    bdatanasc = dados.getString(7);
    bsexo = dados.getString(8);
    bemail = dados.getString(9);

    boolean CampoValido = false;
    TarefaAtualizar atualizar = new TarefaAtualizar(this);
    final String usuario = busuario;

    if(v.getId() == R.id.btneditar1) {
        AcaoEditarCampo(camponome, btneditar2, btnok2);
    }

    if(v.getId() == R.id.btnok1) {
        CampoValido = AcaoOkCampo(camponome, bnome, btnok2, btneditar2);
        if(CampoValido) {
            final String nome = camponome.getText().toString().trim();
            atualizar.execute(usuario, nome, null, null, null, null, null, null);
            txtnomebd.setText(nome);
        }
    }

    if(v.getId() == R.id.btneditar2) {
        AcaoEditarCampo(camposobrenome, btneditar3, btnok3);
    }

    if(v.getId() == R.id.btnok2) {
        CampoValido = AcaoOkCampo(camposobrenome, bsobrenome, btnok3, btneditar3);
        if(CampoValido) {
            final String sobrenome = camposobrenome.getText().toString().trim();
            atualizar.execute(usuario, null, sobrenome, null, null, null, null, null);
            txtsobrenomebd.setText(sobrenome);
        }
    }   

}

private void AcaoEditarCampo(EditText Campo, Button BotaoEditar, Button BotaoOk) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    Campo.setSelectAllOnFocus(true);
    Campo.setFocusable(true);
    Campo.setFocusableInTouchMode(true);            
    Campo.requestFocus();
    imm.showSoftInput(Campo, InputMethodManager.SHOW_FORCED);
    BotaoEditar.setVisibility(View.GONE);
    BotaoOk.setVisibility(View.VISIBLE);
}

private boolean AcaoOkCampo(EditText Campo, String bcampo, Button BotaoOk, Button BotaoEditar) {
    boolean CampoValido = ValidaCampo.validateNotNull(Campo, "Campos sem dados são inválidos!");
    if(CampoValido) {
        if(Campo.getInputType() == (InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS + 1)) {
            CampoValido = ValidaCampo.validateEmail(Campo);
            if(!CampoValido) {
                return CampoValido;
            }
        }           
        if(CampoValido) {
            CampoValido = ValidaCampo.validateNotEqual(Campo, bcampo);
        }
        Campo.setFocusable(false);
        Campo.setFocusableInTouchMode(false);           
        Campo.requestFocus();
        BotaoOk.setVisibility(View.GONE);
        BotaoEditar.setVisibility(View.VISIBLE);
    }

    return CampoValido;
}

Thanks to everyone who has commented and helped with my answer, I will leave this answer unanswered for 2 days in case someone has a better answer.

    
22.01.2016 / 12:31