Non-working if statement
I have this code:
Node main_node = document.getDocumentElement();
NodeList main_node_list = main_node.getChildNodes();
for( int main_iterator = 0; main_iterator < main_node_list.getLength(); main_开发者_如何学运维iterator++ ) {
Node child_node = main_node_list.item( main_iterator );
String child_node_name = child_node.getNodeName();
if( child_node_name == "#text" ) continue;
Toast.makeText( context, "\"" + child_node_name + "\"", Toast.LENGTH_SHORT ).show();
if( child_node_name == "library_visual_scenes" ) {
...
}
}
It can't be any clearer, but in the Toast between the two if statements, it shows that child_node_name is exactly equal "library_visual_scenes" at some point. This without the second if statement being true.
The first if statement, when child_node_name is equal "#text" does get executed. Meaning I never see the text "#text" in the Toast.
child_node_name is a String object. Is this the correct way to compare a String with a character string?
I cannot find out what's going on here. Could this be some Android specific thing, as nearly the same code works if I run it as plain Java on my computer?
Replace
child_node_name == "library_visual_scenes"
child_node_name == "#text"
With
"library_visual_scenes".equals(child_node_name);
"#text".equals(child_node_name);
That is how you must compare Strings in java. "==" compares references, not contents.
Change the following lines to use the equals
method:
if( child_node_name.equals("#text") ) continue;
and
if( child_node_name.equals("library_visual_scenes") ) {
精彩评论