英文:
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("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 (""). 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?
答案1
得分: 2
最好的解决方案是使用按钮数组
ArrayList<Button> btns = new ArrayList<Button>();
英文:
the best solution whould be using an array of Buttons
ArrayList<Button> btns = new ArrayList<Button>();
答案2
得分: 1
你可以将所有按钮添加到一个ArrayList中,然后对其进行操作。
英文:
You can just add all the buttons to an ArrayList and work with it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论