-1

There are two TextView, I want to check the equality of data in these textbox. I wrote one IF function, but the code is running incorrectly. Date time data, date and Time written in the test data I want to check the equality between textler.

TextView textView1 = (TextView) findViewById(R.id.current_time_view);
                String tarihsaatdata = textView1.getText().toString();

                String tarihvesaatText = (tarihTextView.getText().toString()) + " " + (saatTextView.getText().toString());

                if (tarihsaatdata == tarihvesaatText){
                    if(aSwitch.isChecked()){
                    Map<String, Object> newStatus = new HashMap<>();
                    newStatus.put("001", true);

                    ref.setValue(newStatus);
                }else{
                        Map<String, Object> newStatus = new HashMap<>();
                        newStatus.put("001", false);

                        ref.setValue(newStatus);
                    } }else{

                }

            }
        });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
bilgii1
  • 31
  • 6
  • Include your code, screenshots, layout & logcat. This will help in getting a quicker response. You should follow the How To Ask guide before posting a question here: https://stackoverflow.com/help/how-to-ask If your question does not fit into the How To Ask guideline, you can try other programming forums and groups which will help you better than SO. If you've got precise technical problems with your code then post it on SO. If you do not follow the SO questions guideline your question might be downvoted or closed by others with no response. You can edit your question and improve it, though. – MD Naseem Ashraf Mar 12 '19 at 07:49

2 Answers2

2

In Java, two strings (and in general, two objects) must be compared using equals(), not ==.

Using the == operator will compare the references to the strings not the string themselves, whereas the method equals() tests two objects for equality (meaning: testing if two objects have the same value). Almost always you're interested in equality, not in identity.

You have to use equals like this :

if (tarihsaatdata.equals(tarihvesaatText)) {
    // ...
} else {
    // ...
}
milad salimi
  • 1,580
  • 2
  • 12
  • 31
0

if you want to compare two string then use equals().

TextView textView1 = (TextView) findViewById(R.id.current_time_view);
                String tarihsaatdata = textView1.getText().toString();

                String tarihvesaatText = (tarihTextView.getText().toString()) + " " + (saatTextView.getText().toString());

                if (tarihsaatdata.equals(tarihvesaatText)){
                    if(aSwitch.isChecked()){
                        Map<String, Object> newStatus = new HashMap<>();
                        newStatus.put("001", true);

                        ref.setValue(newStatus);
                    }else{
                        Map<String, Object> newStatus = new HashMap<>();
                        newStatus.put("001", false);

                        ref.setValue(newStatus);
                    } }else{

                }

            }
        });
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49