Answer the question
In order to leave comments, you need to log in
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("Ура");
}
Log.d(LOG_TAG, "TAG_NAME: " + tag);
if(tag=="v"){
System.out.println("Ура");
}
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);
}
<?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>
Answer the question
In order to leave comments, you need to log in
if tag is a string then:
tag=="v" is a comparison of object
references tag.equals("v") is a comparison of strings
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)
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 questionAsk a Question
731 491 924 answers to any question