setImageResource from a string

前端 未结 2 1021
遇见更好的自我
遇见更好的自我 2020-12-05 18:50

I would like to change the imageview src based on my string, I have something like this:

ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);

St         


        
2条回答
  •  再見小時候
    2020-12-05 19:31

    I don't know if this is what you had in mind at all, but you could set up a HashMap of image id's (which are ints) and Strings of correct answers.

        HashMap images = new HashMap();
        images.put( "poland", Integer.valueOf( R.drawable.poland ) );
        images.put( "germany", Integer.valueOf( R.drawable.germany ) );
    
        String correctAnswer = "poland";
        imageView1.setImageResource( images.get( correctAnswer ).intValue() );
    

提交回复
热议问题