Note that there are some explanatory texts on larger screens.

plurals
  1. POeclipse for loop assign button strings
    primarykey
    data
    text
    <p>I have been trying to develop an Android app of a simple sound board, which will play several long sounds, and only one at a time, not simultaneously. I have the sound part playing great. I want to assign text for each button. </p> <p>I have a strings.xml set up like so:</p> <pre><code>&lt;string name="quote01"&gt;quote01&lt;/string&gt; &lt;string name="quote02"&gt;quote02&lt;/string&gt; &lt;string name="quote03"&gt;quote03&lt;/string&gt; </code></pre> <p>In my main.xml, I have my buttonIds set up like this:</p> <pre><code> final int[] buttonIds = { R.id.button01, R.id.button02, R.id.button03, R.id.button04, R.id.button05, R.id.button06, R.id.button07, R.id.button08, R.id.button09, R.id.button10, R.id.button11, R.id.button12, R.id.button13, R.id.button14, R.id.button15, R.id.button16, R.id.button16, R.id.button17, R.id.button18, R.id.button19, R.id.button20, R.id.button21, R.id.button22, R.id.button23, R.id.button24, R.id.button25 }; </code></pre> <p>I could just assign the button text like this to get it to work:</p> <pre><code>Button s01 = (Button) findViewById(R.id.button01); s01.setText(this.getString(R.string.quote01)); Button s02 = (Button) findViewById(R.id.button02); s02.setText(this.getString(R.string.quote02)); Button s03 = (Button) findViewById(R.id.button03); s03.setText(this.getString(R.string.quote03)); </code></pre> <p>But I’d rather use a loop to avoid repetitive code. I have a non-working loop like the below:</p> <blockquote> <pre><code>for(int i = 0; i &lt; buttonIds.length; i++) { Button soundButton = (Button)findViewById(buttonIds[i]); soundButton.setText(this.getString(R.string.buttonIds[i]); } </code></pre> </blockquote> <p>But in the 3rd line above, buttonIds can’t be resolved to a field. I have tried putting brackets or parantheis around the buttonIds[1] but my syntax isn’t right. I am sure this is a super easy question, but it is illuding me. Any suggestions?</p> <p>Thanks in advance! Maytag87</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.
    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