if语句不适用于字符串

时间:2016-05-02 19:36:09

标签: android if-statement nfc

在我的项目中,我阅读了NFC标签中的文本,并将其保存为String值。如果字符串值==给定字符串,请执行某些操作。

这是我的代码:

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                if(nfcTag==null){
                    Toast.makeText(context, "Lütfen NFC Etiketini Okutunuz", Toast.LENGTH_SHORT).show();
                }else{
                    nfcString = read(nfcTag);  // this is the part i get the text with read function. i already define nfcString as String.
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            textViewName.setText(nfcString); // it is working fine !

            String salak = textViewName.getText().toString();
            // i created new String because, i tried to get textViewName.getText().toString() , i tought it should be work, but it didn't 

            try {
                // this is the if statement which doesn't work..
                if ( salak == getName(position) ) {
                    // i also tried 
                    // nfcString  == getName(position)
                    // nfcString.toString()  == getName(position).toString()
                    // position thing is about spinner. i use spinner, it works fine also..

                    btn.setText("Girdin!");
                    yoklamaSend(getDersid(position));

                } else
                    Toast.makeText(context, "DERS GİRDİSİ BAŞARISIZ", Toast.LENGTH_LONG).show();
            }
            catch (Exception ee) {
                ee.printStackTrace();

            }
        }
    });

我无法理解为什么它不起作用。 NFC标签工作正常,但不管我做什么,如果声明,我都无法运行。

我从不同应用的样本中获取NFC标签读取代码(工作正常),这里是:

    private String read(Tag tag) throws UnsupportedEncodingException {

    Ndef ndef = Ndef.get(tag);
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();
    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                byte[] payload = ndefRecord.getPayload();
                String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
                int languageCodeLength = payload[0] & 0063;
                return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
            } catch (UnsupportedEncodingException e) {
                Log.e("TAG", "Unsupported Encoding", e);
            }
        }
    }
    return null;
}

3 个答案:

答案 0 :(得分:3)

比较字符串的正确方法是使用.Equals(String)

使用==比较来查看它们是否是内存中的同一对象

请参阅以下链接:

https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

How do I compare strings in Java?

Java String.equals versus ==

答案 1 :(得分:1)

使用equals函数代替==运算符来比较String s。 ==运算符检查两个操作数是否相同object

例如,使用salak.equals(getName(position))代替salak == getName(position)

答案 2 :(得分:0)

尝试在equals()声明中使用==方法代替if

相关问题