Tell me why the ImageView class has an attribute

 private int mResource = 0; 

In which the resource Id is stored:

 public void setImageResource(@DrawableRes int resId) { ... mResource = resId; ... } 

But there is no method to get it? And instead, it is necessary to pervert by adding tags during initialization, and find out the image identifier by them.

  • reflection always helps) - pavel
  • 3
    Because it was not implemented by developers from Google. Your cap. In general, if you need such a method then you are doing something wrong. - xkor

1 answer 1

Google needs to ask about this, but I think I’m not mistaken, assuming that this is due to the fact that not only resources can be assigned an image for ImageView and it was easier to prevent them from asking about this than doing a bunch of checks and returning null / -1 of a similar method.

And yes, if you need this method, then you are doing something wrong. Most likely you just need to get a certain array, where and add the ID your images.

  • Thank. About the fact that it is not there because it is not realized of course understandable. If it is forbidden, then there is a high probability that null / -1 will come, but why is it big? All the same, it is initiated by a specific method. And can you explain why if the method is required, then I do something wrong? For example, I need it to determine the type of image in the RecyclerView.Adapter. I do not need to know what item corresponds to a specific ImageView, for me it seems logical that I just recognize in the properties of the resource id and act accordingly. - Sergey Chalbyshev
  • @SergeyChalbyshev, well, it's just that everyone is already accustomed to not pulling out the resource identifier from the View, you can only pull out the displayed data, so it’s considered that you are doing something wrong. Those. your data should be displayed separately and it is with them that you should operate and not include the View in the logic. - Yuriy SPb