0

when clicking on the button it does nothing ,,,after testing I concluded the problem is with the equal method statment ,,,the whole issue is when comparing string array to string any solutions?

EditText coderead = (EditText)findViewById(R.id.editText1);
        Button   go       = (Button)findViewById(R.id.button1); 
        final String mn=coderead.getText().toString();          
            final String code[] = {"m1","n2"};
            final double pointx[] ={23.666666,65.22222};
            final double pointy[] ={31.55555,29.665544};

        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent transfercode = new Intent(getApplicationContext(), FeenbezabtActivity.class);

                for (int i=0; i<code.length; i++) {
                    if(code[i].equals(mn)) {
                        transfercode.putExtra("lat2", pointx[i]);
                        transfercode.putExtra("long", pointy[i]);
                    startActivity(transfercode);
                    }

                    else{Toast.makeText(getBaseContext(), "code not found", 5000);}
                }
            }
        });
5
  • Have you tried printing the value of mn to see what it is you are trying to compare? Commented Dec 20, 2011 at 17:15
  • If your onClick is really invoked then it's supposed to something since got both if and else (one of them should execute). Right? Commented Dec 20, 2011 at 17:22
  • @DaveJohnston it forwords to the transfer code activity when i dismiss the equals Commented Dec 20, 2011 at 17:22
  • @user1015086 That wasn't his question, his was "are you comparing the values you think you are". Commented Dec 20, 2011 at 17:24
  • @user1015086 what I meant was you should add a statement to your code to print the values of code[i] and mn to the console or file or wherever you are doing your logging and then you will see if the values are what you expect them to be (a very basic debugging technique). Commented Dec 21, 2011 at 10:11

5 Answers 5

2

Your mn variable should be read after your button has been clicked.

Button go  = (Button) findViewById(R.id.button1); 
final String code[] = {"m1", "n2"};
final double pointx[] = {23.666666, 65.22222};
final double pointy[] = {31.55555, 29.665544};

go.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent transfercode = new Intent(getApplicationContext(), FeenbezabtActivity.class);

        // mn should be read after the button click!
        EditText coderead = (EditText) findViewById(R.id.editText1);
        final String mn = coderead.getText().toString();          

        for (int i = 0; i < code.length; i++) {
            if (code[i].equals(mn)) {
                transfercode.putExtra("lat2", pointx[i]);
                transfercode.putExtra("long", pointy[i]);
                startActivity(transfercode);
            } else {
                Toast.makeText(getBaseContext(), "code not found", 5000);
            }
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

@DaveJohnston i didnt understand the meaning of printing,,,,it's more clear saying invoke ....wat do u think?
@user1015086 His suggestion was to see if you were comparing the value against what you thought you were, and when--once you realized you weren't comparing it to the current value of the text box, it would have been much easier to track down. His answer (before this one) also correctly identified the issue. Btw, consider using something other than the default IDs for yur resources.
1

So if I understand your code correctly you are trying to respond to a button click and take the text that has been input and do something based on that?

You are setting the value of mn at the time you are creating the button, rather than when the button is pressed. At that time the text will be empty (or null). You should move the code to get the value of the entered text to within the onClickListener.

Comments

0

Should your "code not found" message happen outside the for loop?

2 Comments

it's about the system code means the input code from the previous activity
Do you want to do that makeText for each iteration of your loop (what you currently have) or only if you exit the loop without finding a match?
0

What do you mean by nothing happens? Do you get a Toast message or not? Did you make sure that no error is being generated? If you are not getting the Toast Message and you have no errors, then make sure the intent is correct. I would recommend you debug the code from the line of
Intent transfercode = new Intent(getApplicationContext(), FeenbezabtActivity.class);
Then, report what is happening back here.

1 Comment

when // to the line of equals compare ..... it proceeds to the feenbezabtActivity!
0

Something I don't get. With these two lines:

    final String mn=coderead.getText().toString();          
        final String code[] = {"m1","n2"};

Why don't you just compute the (final) index to code right then and there, vs waiting until onClick?

1 Comment

the code when is placed in on click ,,,,ide asks for turning those to final:D

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.