How to compare a CharSequence to a String - Android

user2544045 picture user2544045 · Sep 9, 2013 · Viewed 17.2k times · Source

I have a class that places a text into an arrayList. Then it makes all the variables into CharSequences, when I try to compare the variables to a String such as == "test"; it doesn't work here is my code that I use to get the variables

class Item {
            String descs;

            public Item (String descs){

                 this.descs = descs;

                 }


            public CharSequence getDescs() {
                return descs;
            }
        }

This is the code that compares it to the String

 if(p.getDescs().toString() == "trash"){

          descsView.setVisibility(View.GONE);

                            }

          else{
              descsView.setText(p.getDescs());

                            }

I know for a fact that p.getDescs() is equal to trash because when it sets the text for descsView it is set to trash. SO why doesn't the first if statement work?

Answer

Raghunandan picture Raghunandan · Sep 9, 2013

Use .equals or .equalsIgnoreCase to compare strings

     if(p.getDescs().toString().equals("trash"))

Also check this

What is the difference between == vs equals() in Java?