简体   繁体   English

比较字符串数组与Android中的一个字符串?

[英]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);}
                }
            }
        });

Your mn variable should be read after your button has been clicked. 单击按钮后,应读取mn变量。

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

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. 您在创建按钮时而不是在按下按钮时设置mn的值。 At that time the text will be empty (or null). 届时文本将为空(或null)。 You should move the code to get the value of the entered text to within the onClickListener. 您应该将代码移动到onClickListener中以获取输入文本的值。

您的“找不到代码”消息是否应该在for循环之外发生?

What do you mean by nothing happens? 什么都没发生是什么意思? Do you get a Toast message or not? 您是否收到Toast消息? 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. 如果没有收到Toast消息并且没有错误,请确保意图正确。 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. 然后,在这里报告正在发生的事情。

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 ? 您为什么不只计算(final)索引来立即code ,而又要等到onClick呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM