1

Hi I have an array that stores when radio buttons are checked so that when I load the radio settings again I can set the apporpriate one as checked.

My array is like this :

   |0          |1         |2
----------------------------------
0  |Option1    |data      |false
----------------------------------
1  |Option2    |data      |true
----------------------------------
2  |Option3    |data      |false
----------------------------------
3  |Option4    |data      |false

etc....

Now the index I am interested in is the true/false one as that tells me if that radio button should be checked.

I read these values from a database into the array. That's all working fine and the data is in the array correctly.

For some reason when I do a comparison it does not match it up with what I can see is correct.

This is what I am doing in a loop...

String checked = myarray[i][2];

// This correctly outputs 'false', 'true', 'false' and 'false' as per the array above.            
Toast.makeText(DatabaseTestActivity.this, checked, Toast.LENGTH_LONG).show();

// Now when it gets here it fails to trigger the match for 'true'.
if (checked == "true") {
     active = i;
     Toast.makeText(DatabaseTestActivity.this, "Found Check", Toast.LENGTH_LONG).show();
}

Any idea on what I am doing wrong ? It's just a simple string compare, or so I thought ??

2 Answers 2

2

Use String.equals() to compare strings.

if (checked.equals("true")) {

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! That was it - I am too used to PHP for my own good, thank you very much :)
1

You should compare using String#equals or String#equalsIgnoreCase:

checked.equals( "true" )

== compares references, while equals compares the content of the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.