check function always returning true

Asked

Viewed 101 times

1

i have a layout with several edittext (16) and wanted to make a check if it was changed. when I call this layout I load all my edittexts with database data, having the user the option to save/, change page, quit, among others wanted that whenever the user changes and not saved, when leaving that layout a pop up appeared asking if you want to save the changes, as happens 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 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 (already placed inside the box button action so that it only actually runs after pressing the button so that the application does not change the text or cursor location in another Thread)

my typing function

    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 search 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, I really appreciate it. grateful!

2 answers

2

Try changing the command

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

for

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

maybe this will solve.

But I think you should think of a different strategy. How to use a Static or global variable that is changed to true when the person changes Edittext.

Maybe this will help you as an alternative addTextChangedListener

2


Just swap the if’s comparisons of

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

for

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

You can’t 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 of String. Then use the equals class String.

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

Browser other questions tagged

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