Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect the resource for findViewById(R.id.xxx) in code
    primarykey
    data
    text
    <p>This is my first post, sorry for the title and the explication. Sorry for my English.</p> <p>I define an .xml with all I need. I have 10 ImageView and the id for 10 ImageView are myimage01, myimage02, ... , myimage010. I need to select an image for an ImageView. I can do it as follows:</p> <pre><code>String imageName=getImageName(); id = getResources().getIdentifier(imageName, "drawable", getPackageName()); drawable = res.getDrawable(id); ImageView cant1= (ImageView)findViewById(R.id.myimage01); cant1.setImageDrawable(drawable); ImageView cant2= (ImageView)findViewById(R.id.myimage02); cant2.setImageDrawable(drawable); ImageView cant3= (ImageView)findViewById(R.id.myimage03); cant3.setImageDrawable(drawable); ImageView cant4= (ImageView)findViewById(R.id.myimage04); cant4.setImageDrawable(drawable); ImageView cant5= (ImageView)findViewById(R.id.myimage05); cant5.setImageDrawable(drawable); ImageView cant6= (ImageView)findViewById(R.id.myimage06); cant6.setImageDrawable(drawable); ImageView cant7= (ImageView)findViewById(R.id.myimage07); cant7.setImageDrawable(drawable); ImageView cant8= (ImageView)findViewById(R.id.myimage08); cant8.setImageDrawable(drawable); ImageView cant9= (ImageView)findViewById(R.id.myimage09); cant9.setImageDrawable(drawable); ImageView cant10= (ImageView)findViewById(R.id.myimage010); cant10.setImageDrawable(drawable); </code></pre> <p>But this is too bad, its better to use a loop. But I don't know do this.</p> <p>I need something like that:</p> <pre><code> String cad; for(int i=0;i&lt;10;i++){ cad="myimage0"; String cat= Integer.toString(i); cad=cad.concat(cat); ImageView cant1= (ImageView)findViewById(R.id.cad); cant1.setImageDrawable(drawable); } </code></pre> <p>But there is an error in:</p> <pre><code>ImageView cant1= (ImageView)findViewById(R.id.***cad***); </code></pre> <p>Thanks for all</p>
    singulars
    1. This table or related slice is empty.
    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