【问题标题】:Displaying image based on string chosen?根据选择的字符串显示图像?
【发布时间】:2012-08-23 00:48:17
【问题描述】:

我现在有一个代码仪式,它只是从一个数组中生成一个随机的饮料组合,我需要做的是为每个选项分配一个不同的图像并让它显示该图像。 这是我的随机饮料代码:

    if(Vodka.equals(true)){
                final TextView text2 = (TextView) findViewById(R.id.display2);
                randomIndex = random.nextInt(array_city.Vodka.length);
                text2.setText(array_city.Vodka[randomIndex]);
            }

假设这段代码吐出“Smirnof”然后我显示瓶子的图片,它吐出“Sky”然后变成那个瓶子的图片。如果不为每个选项创建一个 if 语句,我将如何做到这一点,我的数组很长,而且我只是希望有一个更简单的方法来做很多 if 语句? 感谢任何人的帮助!非常感谢我已经坚持了一段时间。

================================================ ================= @琼

以下是我尝试使用您的代码汇总的内容:

    //Run option Vodka
            if(Vodka.equals(true)){
                final TextView text2 = (TextView) findViewById(R.id.display2);
                randomIndex = random.nextInt(array_city.Vodka.length);
                text2.setText(array_city.Vodka[randomIndex]);

                final ImageView image = (ImageView) findViewById(R.id.imageView1);
                int Cimage = getResources().getIdentifier(array_city.Vodka[randomIndex], null, "com.famousmods.what.should.i.drink");
                image.setImageResource(Cimage);
            }

这是我的数组的样子(一个较小的示例):

    public static final String[] Vodka = {"Absolut Vodka","Finlandia","Ketel One","Polmos Krakow","Skyy","smirnoff vodka",
    "Stolichnaya","Fleischmann's","Gilbey's","Gordon's","Wolfschmitt","Five-O-Clock"};

我已将文件“smirnoff_vodka.png”作为示例放入我的 res/drawables 中,但它不起作用?

【问题讨论】:

  • 如何加载图像?你怎么知道图像的路径?请提供更多代码。
  • @nkr 如果我理解正确你问的所有图像将在应用程序本地存储?我还没有目录或任何设置
  • 所以你还没有编写任何图像加载代码?
  • @nkr 不,我知道这样做的唯一方法是为数组中的每个选择创建数百个 if 语句,我知道必须有一个更简单的方法,我仍然相当安卓编程新手。这就是我发这篇文章的原因

标签: android arrays string image random


【解决方案1】:

您可以在上下文中使用getResources().getIdentifier("image_name", null, "your_application_package"); 来检索图像ID。然后,您可以像使用 R.id.image_name 一样使用此 id。

编辑:它需要是“可绘制的”而不是 null。见下文。

【讨论】:

  • 很抱歉,但我对此仍然很陌生,仍然感到困惑,您是否可以更详细地解释一下?这真的很有帮助
  • 如果您想避免使用大的switch 语句,您必须定义一个约定并使用其名称自动找到合适的图像。例如,将您的瓶子图片命名为 bottle1.pngbottle2.pngbottle3.png。请注意,123 应该与您的 array_city 数组中这些瓶子的名称索引匹配。这样就可以使用int id = context.getResources().getIdentifier("bottle"+i, null, "your_application_package")获取对应图片的标识符,使用imageView.setImageResource(id)更新ImageView。
  • 使用:getResources().getIdentifier("img"+randomIndex,null, "com.famousmods.what.should.i.drink"); 并调用您的图像img1.png
  • 我将图像重命名为“img1”,这就是其余代码的样子 final ImageView image = (ImageView) findViewById(R.id.imageView1); int Cimage = getResources().getIdentifier("img"+randomIndex, null, "com. Famousmods.what.should.i.drink"); text2.setText(Cimage); image.setImageResource(Cimage); } 它仍然不显示图像?
  • 我还没有测试过,但这里有一些建议: - 将 img1 重命名为 img1.png。 - 您的图片在res/drawable 文件夹中吗? - 检查randomIndex 的值,考虑将其强制为1。 - 将context.getResources().getIdentifier("bottle"+i, null, "your_application_package") 替换为context.getResources().getIdentifier("bottle"+i, "drawable", context.getPackageName())。 - 检查getIdentifier 返回的内容。
猜你喜欢
  • 2013-10-14
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2023-01-25
  • 2013-12-24
  • 2017-01-12
  • 2013-11-21
  • 1970-01-01
相关资源
最近更新 更多