使用for循环和ArrayList的内容设置多个按钮的文本 – Java – Android

huangapple go评论74阅读模式
英文:

How to set text of multiple buttons using a for loop and the contents of an ArrayList - Java - Android

问题

I currently have 35 buttons, not all of which will be used by the final product.
I also have an arraylist with a variety of items:

ArrayList textBlobs = new ArrayList();
textBlobs.add("text");
textBlobs.add("two");
textBlobs.add("tt");
textBlobs.add("txt");
textBlobs.add("te");
textBlobs.add("tet");
textBlobs.add("go");
textBlobs.add("eat");
textBlobs.add("spring");
textBlobs.add("rolls");
textBlobs.add("egu77$");

I also have 35 buttons called buttonx with x being a number 0 through 34 (35 total)

private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13,
        button14, button15, button16, button17, button18, button19, button20, button21, button22, button23, button24, button25, button26, button27,
        button28, button29, button30, button31, button32, button33, button34;

These buttons have already been initialized and declared using root.findByViewId(R.id.button_)

Here I have a for loop that simply runs through the textBlobs ArrayList. As of right now, all the buttons have no text (""), and I want to use this for loop to set buttons 0 through textBlobs.size() to the text contained within textBlobs at that index.

for(int i = 0; i < textBlobs.size(); i++ ) {

        }

Not all of the buttons will be used, and that is the goal. This should be able to be used with an ArrayList of any length up to 35. I am stuck as to how I should be setting the text. As far as I know, the only way to set the text of a button is to use

button0.setText("insert text");

which would not work in my case because I cannot make the number part of the name change between each instance of the for loop.

Does anyone know how I can accomplish my goal?

英文:

I currently have 35 buttons, not all of which will be used by the final product.
I also have an arraylist with a variety of items:

ArrayList textBlobs = new ArrayList();
textBlobs.add(&quot;text&quot;);
textBlobs.add(&quot;two&quot;);
textBlobs.add(&quot;tt&quot;);
textBlobs.add(&quot;txt&quot;);
textBlobs.add(&quot;te&quot;);
textBlobs.add(&quot;tet&quot;);
textBlobs.add(&quot;go&quot;);
textBlobs.add(&quot;eat&quot;);
textBlobs.add(&quot;spring&quot;);
textBlobs.add(&quot;rolls&quot;);
textBlobs.add(&quot;egu77$&quot;);

I also have 35 buttons called buttonx with x being a number 0 through 34 (35 total)

private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13,
        button14, button15, button16, button17, button18, button19, button20, button21, button22, button23, button24, button25, button26, button27,
        button28, button29, button30, button31, button32, button33, button34;

These buttons have already been initialized and declared using root.findByViewId(R.id.button_)

Here I have a for loop that simply runs through the textBlobs ArrayList. As of right now, all the buttons have no text (""). I want to use this for loop to set buttons 0 through textBlobs.size() to the text contained within textBlobs at that index.

for(int i = 0; i &lt; textBlobs.size(); i++ ) {

        }

Not all of the buttons will be used and that is the goal, this should be able to be used with an ArrayList of any length up to 35. I am stuck as to how I should be setting the text. As far as I know the only way to set the text of a button is to use

button0.setText(&quot;insert text&quot;);

which would not work in my case because I cannot make the number part of the name change between each instance of the for loop.

Does anyone know how I can accomplish my goal?

答案1

得分: 2

最好的解决方案是使用按钮数组

ArrayList<Button> btns = new ArrayList<Button>();
英文:

the best solution whould be using an array of Buttons

ArrayList&lt;Button&gt; btns = new ArrayList&lt;Button&gt;();

答案2

得分: 1

你可以将所有按钮添加到一个ArrayList中,然后对其进行操作。

英文:

You can just add all the buttons to an ArrayList and work with it.

huangapple
  • 本文由 发表于 2020年8月11日 02:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定