Pergunta

Why is this False? I can't get it. DataBaseHelper.getBoolean(); is a string value. In this if statement it outputs the same result but it says it is not equal with each other..

            String a = DataBaseHelper.getBoolean(id);
            String b = DataBaseHelper.getBoolean(id);
            if (a==b){
                newTextView.setText(DataBaseHelper.getBoolean(id) + " == " + DataBaseHelper.getBoolean(id) + " is TRUE \n");
            } else {
                newTextView.setText(DataBaseHelper.getBoolean(id) + " == " + DataBaseHelper.getBoolean(id) + " is FALSE \n");
            }

getBoolean method from toher class.

public String getBoolean(int randomIndex) {
        // TODO Auto-generated method stub
        open();
        Cursor c = myDataBase.query(TABLE_NAME1, columns, WHATTODONOW_COLUMN_ID
                + "=" + randomIndex, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
            String text = c.getString(8);
            return text;
        }
        closee();
        return null;
    }
Foi útil?

Solução

because String a and String b objects are different (== test for reference equality). you should compare String instance through equals

if (a.equals(b)) {}

Outras dicas

You should use a.equals(b) or a.equalsIgnoreCase(b) instead of == operator to compare String. == operator compares the references of the two operands. Your a and b are different String objects hence it fails .

Try with one of following

if (a.equals(b)) {}

And

if (a.equalsIgnoreCase(b)) {}

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top