N
N
Nikita Sklyuev2012-06-06 17:29:33
Java
Nikita Sklyuev, 2012-06-06 17:29:33

Why doesn't string comparison work?

Hello dear habrchane.
Learn the basics of Java and programming for Android.
I ran into a very interesting problem.
I do the following - using XmlPullParser I parse the XML document, going through all the elements, since there are a lot of elements, I need to compare the names of the elements. So, while reading xml, I enter the names of the tags into a variable, and compare, for example:

if(tag=="v"){
            System.out.println("Ура");
          }

But the comparison doesn't work for the life of me.
I logged this variable to see what was in it, and I was surprised that it was v
Log.d(LOG_TAG, "TAG_NAME: " + tag);
if(tag=="v"){
            System.out.println("Ура");
          }

And so a question why comparison does not work I can not understand in any way.
The comparison does not work with either the name of the tags or the name of the attributes, in general!
Help me please!
Below is the entire code.
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et=(EditText) findViewById(R.id.editText1);
    // находим список
    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    
    String tmp = "";
    
    

    try {
      XmlPullParser xpp = prepareXpp();

      while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {

        switch (xpp.getEventType()) {
        // начало документа
        case XmlPullParser.START_DOCUMENT:
          Log.d(LOG_TAG, "START_DOCUMENT");
          break;
        // начало тэга
        case XmlPullParser.START_TAG:
          
          tag=xpp.getName();
          tag=tag.trim();
          et.setText(tag);
          
          
          tmp = "";
          System.out.println(tag);
          if(tag=="v"){
            System.out.println("Ура");
          }
          for (int i = 0; i < xpp.getAttributeCount(); i++) {
            name=xpp.getAttributeValue(i);
          }

          if (!TextUtils.isEmpty(tmp))
            Log.d(LOG_TAG, "Attributes: " + tmp);
          break;
        // конец тэга
        case XmlPullParser.END_TAG:
          Log.d(LOG_TAG, "END_TAG: name = " + xpp.getName());
          name="";
          istitle = false;
          break;
        // содержимое тэга
        case XmlPullParser.TEXT:
          if (tag=="v") {
            output.add(xpp.getText());
          }else{
            Log.d(LOG_TAG, "TAG_NAME: " + tag);
          }
          
          break;

        default:
          break;
        }
        // следующий элемент
        xpp.next();
      }
      Log.d(LOG_TAG, "END_DOCUMENT");

    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, output);

    // присваиваем адаптер списку
    lvMain.setAdapter(adapter);
  }

  XmlPullParser prepareXpp() {
    return getResources().getXml(R.xml.data);
  }

Well, actually the xml file itself:
<?xml version="1.0" encoding="utf-8"?>
<server xmlns:f="http://www.mysite.ru" >
    <call-r>
        <f:getProjects u="1" >
            <v-r>
                <v>
                    <v k="id" >1176004</v>
                    <v k="name" >какое то описание</v>

                    <v k="descr" ></v>
                    <v k="category" >2</v>

                    <v k="subcategory" >27</v>
                    <v k="city" ></v>
                    <v k="country" ></v>
                    <v k="cost" >50</v>
                    <v k="kind" >1</v>
                    <v k="offers_count" >5</v>
                    <v k="pro_only" >1</v>
                    <v k="post_date" >1338210266</v>
                    <v k="currency" >0</v>
                    <v k="logo" ></v>
                    <v k="is_pro" >0</v>
                    <v k="priceby" >4</v>
                    <v k="prefer_sbr" >0</v>
                </v>
                <v>
                    <v k="id" >1176004</v>
                    <v k="name" >какое то описание</v>

                    <v k="descr" ></v>
                    <v k="category" >2</v>

                    <v k="subcategory" >27</v>
                    <v k="city" ></v>
                    <v k="country" ></v>
                    <v k="cost" >50</v>
                    <v k="kind" >1</v>
                    <v k="offers_count" >5</v>
                    <v k="pro_only" >1</v>
                    <v k="post_date" >1338210266</v>
                    <v k="currency" >0</v>
                    <v k="logo" ></v>
                    <v k="is_pro" >0</v>
                    <v k="priceby" >4</v>
                    <v k="prefer_sbr" >0</v>
                </v>
            </v-r>
        </f:getProjects>
        <f:getProjects u="2" >
            <v-r>
                <v>
                    <v k="id" >1176004</v>
                    <v k="name" >какое то описание</v>

                    <v k="descr" ></v>
                    <v k="category" >2</v>

                    <v k="subcategory" >27</v>
                    <v k="city" ></v>
                    <v k="country" ></v>
                    <v k="cost" >50</v>
                    <v k="kind" >1</v>
                    <v k="offers_count" >5</v>
                    <v k="pro_only" >1</v>
                    <v k="post_date" >1338210266</v>
                    <v k="currency" >0</v>
                    <v k="logo" ></v>
                    <v k="is_pro" >0</v>
                    <v k="priceby" >4</v>
                    <v k="prefer_sbr" >0</v>
                </v>
                <v>
                    <v k="id" >1176004</v>
                    <v k="name" >какое то описание</v>

                    <v k="descr" > </v>
                    <v k="category" >2</v>

                    <v k="subcategory" >27</v>
                    <v k="city" ></v>
                    <v k="country" ></v>
                    <v k="cost" >50</v>
                    <v k="kind" >1</v>
                    <v k="offers_count" >5</v>
                    <v k="pro_only" >1</v>
                    <v k="post_date" >1338210266</v>
                    <v k="currency" >0</v>
                    <v k="logo" ></v>
                    <v k="is_pro" >0</v>
                    <v k="priceby" >4</v>
                    <v k="prefer_sbr" >0</v>
                </v>
            </v-r>
        </f:getProjects>

        <f:getMessFolders>
            <v-r>
                <v>
                    <v k="id" >24905</v>
                    <v k="fname" >offtop</v>
                </v>
            </v-r>
        </f:getMessFolders>
    </call-r>

</server>

I would be very grateful for help. I'm just completely at a loss why the strings are not compared, because both there and there v
Maybe I stepped somewhere? Is it the end of the working day? If so, please tell me where?
PS Yes, the above is shitty code, don't pay attention to it, this is a test project, I'm getting used to Java and Android
Thanks in advance to everyone!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dewen, 2012-06-06
@trilodi

if tag is a string then:
tag=="v" is a comparison of object
references tag.equals("v") is a comparison of strings

0
0leGG, 2012-06-06
@0leGG

In addition to what was said above, I would say that in the case of Android, it is better to use the TextUtils.equals() method, in which case there is no risk of getting a NullPointerException. Or, alternatively, put a string constant on the left: "text".equals(value)

R
rvller, 2012-06-06
@rvller

Strings are compared via s.equals().

N
Nikita Sklyuev, 2012-06-06
@trilodi

Thank you very much. Just like I wrote, I’m just getting acquainted with yavri

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question