2

I have a check box and according to its status value of the code variable should change. Because I'm using it to next activity.

I have code as shown below,

String code;
String itemCode;
String MayoItemCode;

mayoBaseCB.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (mayoBaseCB.isChecked()) {
                    code = (MayoItemCode);

                } else {
                    code.equals(itemCode);
                }
            }

        });

itemcode and MayotItemCode has values and I want to assign it to the variable code according to the status of the checkbox.

I have tried these 2 ways (equaling) but it didn't work for me. Can anyone help me to solve this?

3
  • Shouldn't you be assigning in your else block? Commented Apr 7, 2015 at 11:01
  • what happens in your next activity does it get the String var as null ? Commented Apr 7, 2015 at 11:11
  • I got it as null, but its working now, thanks alot Commented Apr 7, 2015 at 11:16

2 Answers 2

4

You are invoking code.equals, which checks for String content equality and returns boolean (you are not using the outcome of the invocation).

Not sure I understand the question but you might want to assign instead, with the = idiom.

I.e.

} else {
    code = itemCode;
}

Edit

On second thought, since you are using an anonimized idiom, you need your fields outside your anonymous View.OnClickListener to be final.

Yet again, assignment would not compile as illustrated in your example, with final fields.

What you may want to use is a mutable CharSequence (i.e. a StringBuilder), and manipulate it through method invocation.

Sounds fishy though, but there's not enough information (and scope) to help you further.

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

1 Comment

When I changed the else block it worked, thanks alot for the explanation a s well
0

You Should replace you if block with:

if (mayoBaseCB.isChecked()) {
                code = MayoItemCode;

            } else {
                code = itemCode;
            }

.equals() method just used for comparision not for assigning value

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.