Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Views.IDs & Drawable.Ints in a loop
    text
    copied!<p>Well, this is my first topic around here, but that shouldn't matter so far. </p> <p>My question is: How can I handle Views with similar IDs (example: R.id.View1, View2, View3, ..) in a loop &amp; how can I give each of them a different picture out of my drawable resource (example: R.drawable.pic1, pic2, pic3, ..)?</p> <p>I haven't found an answer on this yet, thought I didn't rly know what to look for in google :P.</p> <p>To give you a better view about my topic, here is my code:</p> <pre><code> &lt;LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="492px" &gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:id="@+id/puzzletile_r1_c1" android:layout_width="82px" android:layout_height="82px" /&gt; &lt;ImageView android:id="@+id/puzzletile_r1_c2" android:layout_width="82px" android:layout_height="82px" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:id="@+id/puzzletile_r2_c1" android:layout_width="82px" android:layout_height="82px" /&gt; &lt;ImageView android:id="@+id/puzzletile_r2_c2" android:layout_width="82px" android:layout_height="82px" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>There're alot more Linears &amp; ImageViews inside that file, but I deleted them for a better overview.</p> <pre><code>package fast.games.puzzleme; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class GameActivity extends Activity{ // Declare variable for checking if the custom Title is supported boolean customTitleSupported; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.playground); } } </code></pre> <p>My general idea was to do sth like this:</p> <pre><code>for(i = 0; i&lt;= 10; i++){ Imageview iv = (ImageView)this.findViewById(R.id.pic_+i); iv.setImageResource(R.drawable.img_+i) } </code></pre> <p>Thought it doesn't work like that >.&lt;</p> <p>Is there any way to accomplish that?</p> <p>Thanks!</p>
 

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