简体   繁体   English

在ImageView上设置后获取drawable的名称

[英]Get the drawable's name after setting it on ImageView

I'am Actually setting an image to the ImageView Dynamically from the Drawable folder. 我实际上是从Drawable文件夹动态地将图像设置为ImageView。 I got the working code to set the image dynamically but I can't get the name of the image/drawable which gets set on the image view at the moment. 我得到了动态设置图像的工作代码但是我无法获得此时在图像视图上设置的图像/可绘制的名称。 Either I should get the drawable name or any code to get the instance of each drawable with a name which I can set on the ImageView. 要么我应该得到可绘制的名称或任何代码来获取每个drawable的实例与我可以在ImageView上设置的名称。 Any help is Much Appreciated!! 任何帮助深表感谢!!

I searched and tried all answers on StackOverflow. 我在StackOverflow上搜索并尝试了所有答案。

Set the image dynamically to the imageview and also get that image's name. 将图像动态设置为imageview,并获取该图像的名称。

As a good practice, you can store the drawable in a variable and update the imageview. 作为一种好的做法,您可以将drawable存储在变量中并更新imageview。

@DrawableRes int myDrawable;
iv.setImageDrawable(getResources().getDrawable(myDrawable));

Setting tag and getting from tag is no ideal imho because it's more maintainable and debuggable. 设置标签和从标签获取并不是理想的imho,因为它更易于维护和调试。

Ok. 好。 after 2 days of research and trials, I finally managed to do what I planned to do. 经过2天的研究和试验,我终于设法做了我打算做的事情。 Here's my code, if anybody gets help from it. 这是我的代码,如果有人从中得到帮助。

ArrayList<Integer> array_fruits = new ArrayList<Integer>();
    int fruit_apricot = R.drawable.fruits_apricot;
    int fruit_apple = R.drawable.fruits_apple;
    int fruit_banana = R.drawable.fruits_banana;

.... and more .... 和更多

array_fruits.add(fruit_apple);
        array_fruits.add(fruit_apricot);
        array_fruits.add(fruit_banana);

... and more ... 和更多

//WORKING CODE TO GENERATE NUMBER BETWEEN 30 AND 1 (0 to 29 in arraylist) //工作代码生成30和1之间的数字(arraylist中为0到29)

Random rand = new Random();
                    int num = rand.nextInt(30) + 1;
                    array_fruits.add(num);
                    itemImg.setImageDrawable(getResources().getDrawable(array_fruits.get(num)));
                    Log.e("num: ", num+"");
                    Toast.makeText(getApplicationContext(), num+"", Toast.LENGTH_LONG).show();

I had tried almost all solutions given on stackoverflow and other sites. 我曾经尝试过在stackoverflow和其他网站上提供的几乎所有解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM