Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying image based on string chosen?
    primarykey
    data
    text
    <p>I have a code rite now that just generates a random drink combination from an array, what I need to do is have a different image assigned to each choice and have it display that image. Here is my code for a random drink:</p> <pre><code> 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]); } </code></pre> <p>Say this code spits out "Smirnof" then i display a picture of the bottle, it spits out "Sky" then changes to a picture of that bottle. How would i do this without making an if statement for each option, my arrays are very long and that would be alot of if statements i was just Hoping that there is an easier way to do it? Thanks anybody for your help! it is very much appreciated I have been stuck on this for a while.</p> <p>=============================================================== @Joan</p> <p>Here is what i am trying to put together using your code:</p> <pre><code> //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); } </code></pre> <p>Here is what my array looks like (a smaller example):</p> <pre><code> 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"}; </code></pre> <p>I have put the file "smirnoff_vodka.png" into my res/drawables as an example but it doesnt work?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload