There is a code:

if (newses.get(position).getTitle().endsWith("doc")) { holder.title.setText("{fa-file-word-o}"); holder.title.setTextColor(Color.parseColor("#42aaff")); } else if (newses.get(position).getTitle().endsWith("jpg")) { //Glide.with(context).load("http://nhmt.ru"+newses.get(position).getUrl()).into(holder.mCircle); //Log.i(newses.get(position).getUrl(),"Nags"); //holder.title.setVisibility(View.GONE); holder.title.setText("{fa-file-image-o}"); holder.title.setTextColor(0xffa1887f); } else if (newses.get(position).getTitle().endsWith("jpeg")) { holder.title.setText("{fa-file-image-o}"); holder.title.setTextColor(0xffa1887f); } else if (newses.get(position).getTitle().endsWith("gif")) { holder.title.setText("{fa-file-image-o}"); holder.title.setTextColor(0xffa1887f); } else if (newses.get(position).getTitle().endsWith("bmp")) { holder.title.setText("{fa-file-image-o}"); holder.title.setTextColor(0xffa1887f); } else { holder.title.setText("{fa-file-o}"); holder.title.setTextColor(0xff90a4ae); } 

How can I reduce this, because for all image files I use the same parameter. Is it possible to cut only this way?

 if(newses.get(position).getTitle().endsWith("gif") ||newses.get(position).getTitle().endsWith("jpg")||newses.get(position).getTitle().endsWith("bmp")){ 

Or somehow in a cycle to fill in an array?

  • one
    At least this "newses.get (position) .getTitle ()" has been converted to a new one. String title = newses.get (position) .getTitle (). and worked with him. - Denis Kotlyarov
  • @DenisKotlyarov, well, yes, I didn’t think. But the main thing is that I have less if there will stretch the whole page - Anret
  • And so, this is not a problem, the main thing in this huge code is to prevent mistakes. And even transfer to String title especially and not necessary. - Denis Kotlyarov

2 answers 2

as an option, you can use the good old switch :

  switch (getExtension(newses.get(position).getTitle())) { case "gif": case "jpeg": case "jpg": case "bmp": holder.title.setText("{fa-file-image-o}"); holder.title.setTextColor(0xffa1887f); break; case "doc": holder.title.setText("{fa-file-word-o}"); holder.title.setTextColor(Color.parseColor("#42aaff")); break; default: holder.title.setText("{fa-file-o}"); holder.title.setTextColor(0xff90a4ae); break; } 
  • Again, for android is not a ride. - iksuy
  • Is there a switch in the android? I doubt very much - Artem Konovalov
  • switch for string values ​​appeared in Java 7 - iksuy
  • and there that the sixth? hard for you guys, but you hold on). - Artem Konovalov
  • yes, java 6. was originally castrated though I found this: stackoverflow.com/questions/20480090/… , so I wasn’t really right :) - iksuy

I would do something like that. You should also put the constants like {fa-file-image-o} and 0xffa1887f into separate variables to improve the readability of the code.

 String[] extensions = new String[]{"jpg", "jpeg", "gif", "bmp"}; int color; String text; if(Stream.of(extensions).anyMatch(s -> newses.get(position).getTitle().endsWith(s))){ text = "{fa-file-image-o}"; color = 0xffa1887f; } else if (newses.get(position).getTitle().endsWith("doc")) { text = "{fa-file-word-o}"; color = Color.parseColor("#42aaff"); } else { text = "{fa-file-o}"; color = 0xff90a4ae; } holder.title.setText(text); holder.title.setTextColor(color); 

PS if this is the code for Android and libraries emulating Stream API you do not use, then instead

 Stream.of(extensions).anyMatch(s -> newses.get(position).getTitle().endsWith(s) 

can be perverted:

 Arrays.asList(extensions).contains(getExtension(newses.get(position).getTitle())); 

where getExtension can be taken from apache.commons.io

 public static String getExtension(String filename) { if (filename == null) { return null; } int index = filename.lastIndexOf("."); if (index == -1) { return ""; } else { return filename.substring(index + 1); } } 
  • Looks cool, thanks))) for the sake of one class you should not connect the library? it's better to cut it because there is not only doc there is docx - Anret
  • Yes, the library should not be connected, you can just copy the paste yourself. - iksuy