verification function always returning true

1

I have a layout with several edittext (16) and wanted to do a check if it was changed. when I call this layout I load all my edittexts with data from the database, having the user the option to save /, change pages, exit, among others I wanted that whenever the user changes and does not save, at the time of leaving that layout appeared a pop up asking if you want to save the changes, as in programs like word, paint and etc.

My problem is that my verification code always returns true regardless of whether it has change or not (true for changes). I'll post the perforated code.

main:

buscardados();
escrevebotao();

...

btnext2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(verificacao())
            {
                AlertDialog.Builder mensagem = 
                        new AlertDialog.Builder(paginadeedicao.this);
                mensagem.setTitle("Atenção!");
                mensagem.setMessage("Deseja salvar as alterações?");

                mensagem.setPositiveButton("Sim",new      'DialogInterface.OnClickListener() {'
                    public void onClick(DialogInterface dialog,int id) {
                        salvar();
                        acoes();

                    }});
                mensagem.setNegativeButton("Não",new      'DialogInterface.OnClickListener() {'
                    public void onClick(DialogInterface dialog,int id) {
                        acoes();

                    }});
                mensagem.show();

button actions (I already put it inside the action of the box button so that it only really executes after pressing the button so that the application does not change the text or location of the cursor on another Thread)

My function writes button

    private void escrevebotao() {
    etdescricao.setText(retornadado("bt"));

    et01.setText(retornadado("bt01"));

    et02.setText(retornadado("bt02"));

    et03.setText(retornadado("bt03"));

    et04.setText(retornadado("bt04"));

    et05.setText(retornadado("bt05"));

    et06.setText(retornadado("bt06"));

    et07.setText(retornadado("bt07"));

    et08.setText(retornadado("bt08"));

    et09.setText(retornadado("bt09"));

    et10.setText(retornadado("bt10"));

    et11.setText(retornadado("bt11"));

    et12.setText(retornadado("bt12"));

    et13.setText(retornadado("bt13"));

    et14.setText(retornadado("bt14"));

    et15.setText(retornadado("bt15"));



}

My function returns given

    public String retornadado(String x){
    String dado = cursor.getString(cursor.getColumnIndex(x));
    return dado;
}

My function fetch data

public boolean buscardados(){
        try{
            cursor = Banco.query("pages",
                    new String [] {"bt","bt01","bt02","bt03","bt04","bt05",
                    "bt06","bt07","bt08","bt09","bt10",
                    "bt11","bt12","bt13","bt14","bt15"}
            , null, null, null, null, null);

            if (cursor.getCount() != 0){
                cursor.moveToFirst();


            }

            return true;
        }
        catch(Exception erro){
            Exibirmensagem("BANCO", "erro ao buscar no banco: "+ erro.getMessage(), "ok");
            return false;
        }
    }

My verification function

public boolean verificacao(){
    boolean flag=false;
    if(String.valueOf(etdescricao.getText())!= retornadado("bt"))
        flag = true;
    else if(String.valueOf(et01.getText())!= retornadado("bt01"))
        flag = true;
    else if(String.valueOf(et02.getText())!=retornadado("bt02"))
        flag = true;
    else if(String.valueOf(et03.getText())!=retornadado("bt03"))
        flag = true;
    else if(String.valueOf(et04.getText())!=retornadado("bt04"))
        flag = true;
    else if(String.valueOf(et05.getText())!=retornadado("bt05"))
        flag = true;
    else if(String.valueOf(et06.getText())!=retornadado("bt06"))
        flag = true;
    else if(String.valueOf(et07.getText())!=retornadado("bt07"))
        flag = true;
    else if(String.valueOf(et08.getText())!=retornadado("bt08"))
        flag = true;
    else if(String.valueOf(et09.getText())!=retornadado("bt09"))
        flag = true;
    else if(String.valueOf(et10.getText())!=retornadado("bt10"))
        flag = true;
    else if(String.valueOf(et11.getText())!=retornadado("bt11"))
        flag = true;
    else if(String.valueOf(et12.getText())!=retornadado("bt12"))
        flag = true;
    else if(String.valueOf(et13.getText())!=retornadado("bt13"))
        flag = true;
    else if(String.valueOf(et14.getText())!=retornadado("bt14"))
        flag = true;
    else if(String.valueOf(et15.getText())!=retornadado("bt15"))
        flag = true;

    return flag;
}

If anyone can help me thank you too much. grateful!

    
asked by anonymous 24.10.2014 / 21:16

2 answers

2

Just change the if's comparisons of

String.valueOf(et15.getText())!=retornadado("bt15")

by

! String.valueOf(et15.getText()).equals(retornadado("bt15"))

You can not compare String's or any other Object (except in cases of autoboxing and unboxing ) using the == operator, because it is comparing memory addresses and not content String . Then use equals of class String .

There are specific cases where using the == operator works, but only for String's that are in Pool de Strings .

    
24.10.2014 / 22:03
2

Try changing the command

String.valueOf(et15.getText())!=retornadado("bt15")

for

false == retornadado("bt15").equals(String.valueOf(et15.getText()

Maybe that'll solve.

But I think you should think about a different strategy. How to use a static or global variable that is changed to true when the person changes the EditText.

Maybe this will help you as an alternative addTextChangedListener

    
24.10.2014 / 21:59