Ti7a
Ti7a

Reputation: 101

comparing array of strings to one string in android?

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);}
                }
            }
        });

Upvotes: 0

Views: 2570

Answers (5)

Hot Licks
Hot Licks

Reputation: 47729

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?

Upvotes: 0

Marcelo
Marcelo

Reputation: 11308

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);
            }
        }
    }
});

Upvotes: 2

DaveJohnston
DaveJohnston

Reputation: 10151

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.

Upvotes: 1

Adel Boutros
Adel Boutros

Reputation: 10285

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.

Upvotes: 0

ewan.chalmers
ewan.chalmers

Reputation: 16235

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

Upvotes: 0

Related Questions