This question has already been answered:

Why does the comparison (if) of identical strings show false ?

Code :

 File sdPath = Environment.getExternalStorageDirectory(); sdPath = new File(sdPath.getAbsolutePath() + "/папка"); ArrayList<File> files = listFilesWithSubFolders(sdPath); for (File fl: files) { format = fl.getName().substring(fl.getName().lastIndexOf(".")); if(format == ".cfg"){ }else if (format == ".png") { ImageView mImageView; mImageView = (ImageView) findViewById(R.id.imageView2); mImageView.setImageBitmap(BitmapFactory.decodeFile(fl.getPath())); } } 

For clarity, the screen so that they do not tell me that my lines are different: D:

enter image description here

Reported as a duplicate by YurySPb. android 24 May '16 at 13:50 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Your lines are different. RTFM equals . - Yura Ivanov
  • if (format.equals (". png")) like this is the right way to compare strings - Andrey Kasyanov
  • Thank you for your attention :) - alex-rudenkiy

1 answer 1

Because strings should not be compared using == , but .equals() .

Read about the comparison of simple and reference types. In your case, everything shows correctly, the result is false

  • one
    Thank you, I choto stupid, or rather forgot about equals . FacePalm :) - alex-rudenkiy
  • for fans == there is also String.intern () - Stranger in the Q