Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to put values of buttons in an array of TextViews in Android
    text
    copied!<p>I have 6 <code>Button</code>s and 6 <code>TextView</code>s that was put in 2 different arrays. what I want to do is when I click a <code>Button</code> the value of the <code>Button</code> will be set on the first <code>TextView</code> and the next <code>Button</code> I click will be set on the next <code>TextView</code> and so on. I don't know the right way to do this. I know the code below was wrong since I set the 'x' to '0' but I'm stuck so please help.</p> <pre><code> Button btn1 = (Button) findViewById(R.id.btn1); Button btn2 = (Button) findViewById(R.id.btn2); Button btn3 = (Button) findViewById(R.id.btn3); Button btn4 = (Button) findViewById(R.id.btn4); Button btn5 = (Button) findViewById(R.id.btn5); Button btn6 = (Button) findViewById(R.id.btn6); Button submit = (Button) findViewById(R.id.btnsubmit); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn1.setText("A"); btn2.setText("M"); btn3.setText("O"); btn4.setText("N"); btn5.setText("L"); btn6.setText("S"); submit.setText("Submit"); } public void onClick(View v) { final TextView[] textView = new TextView[6]; textView[0] = (TextView)findViewById(R.id.t1); textView[1] = (TextView)findViewById(R.id.t2); textView[2] = (TextView)findViewById(R.id.t3); textView[3] = (TextView)findViewById(R.id.t4); textView[4] = (TextView)findViewById(R.id.t5); textView[5] = (TextView)findViewById(R.id.t6); final Button button[] = new Button[6]; button[0] = (Button)findViewById(R.id.btn1); button[1] = (Button)findViewById(R.id.btn2); button[2] = (Button)findViewById(R.id.btn3); button[3] = (Button)findViewById(R.id.btn4); button[4] = (Button)findViewById(R.id.btn5); button[5] = (Button)findViewById(R.id.btn6); int x=0; for(int i = 0; i &lt;6; i++){ if (button[i].getId() == v.getId()){ textView[x].setText(button[i].getText()); } } } </code></pre>
 

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