0

I´ve got an onCheckedChanged that tells me if a RadioButton in a RadioGroup is pushed.

RadioGroup rGroup = (RadioGroup)findViewById(R.id.rdgroup);
        // This will get the radiobutton in the radiogroup that is checked
        RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());

        rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup rGroup, int checkedId)
            {
                // This will get the radiobutton that has changed in its check state
                RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
                // This puts the value (true/false) into the variable
                boolean isChecked = checkedRadioButton.isChecked();
                // If the radiobutton that has changed in check state is now checked...
                if (isChecked)
                {
                    String tmp = checkedRadioButton.getText().toString();
                    //Toast t = Toast.makeText(NeuesKind.this, checkedRadioButton.getText(), Toast.LENGTH_SHORT);
                    //
                   // t.show();
                    if(tmp == "Männlich"){
                        geschlecht = "männlich";
                    }
                    if(tmp == "Weiblich"){
                        geschlecht = "weiblich";
                    }

                    Toast t1 = Toast.makeText(NeuesKind.this, geschlecht, Toast.LENGTH_SHORT);
                    t1.show();
//                  Toast t = Toast.makeText(NeuesKind.this, checkedRadioButton.getText(), Toast.LENGTH_SHORT);
//                  t.show();
                }
            }
        });

When I use the first Toast that is outcommented now, it tells me that tmp is "Männlich" or "Weiblich". When I use the second Toast t1, i tells me geschlecht is empty. The declaration of geschlecht is at the very top of my class because I also need it in my onCreate class.

Why does geschlecht doesn´t take the value of tmp?

user896692
  • 2,351
  • 7
  • 36
  • 57

3 Answers3

3

In Java, doing == means that it will compare the references of the two objects. You need to actually compare the text in the objects by calling the string equals method, like this:

                if (tmp.equals("Männlich")) {
                    geschlecht = "männlich";
                }

                if (tmp.equals("Weiblich")) {
                    geschlecht = "weiblich";
                }

However, it would also be far easier for you to do this:

geschlecht = tmp.toLowerCase(); // toLowerCase will make all the characters lowercase (as you've done in your if block)
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
1

You are not comparing the String contents with your code, but rather the object, use equals or equalsIgnoreCase, like so:

if ("Männlich".equalsIgnoreCase(tmp)) {
    geschlecht = "männlich";
}
if ("Weiblich".equalsIgnoreCase(tmp)) {
    geschlecht = "weiblich";
}
kaderud
  • 5,457
  • 2
  • 36
  • 49
1

Use tmp.compareTo ( "Mannlich" ) not tmp ==

Sudhee
  • 704
  • 6
  • 12