0

Please help I am new to java and android.

Here is the example of the string array resource

strings.xml

<resources>
   <string-array name="dance_steps">
       <item>Step1</item>
       <item>Step2</item>
       <item>Step3</item>
       <item>Step4</item>
   </string-array>
</resources>

How can I get the Strings and put it on different TextView in the java code?

 TextView step1, step2, step3, step4;
 step1 = (TextView) findViewById(R.id.textview1);
 step2 = (TextView) findViewById(R.id.textview2);
 step3 = (TextView) findViewById(R.id.textview3);
 step4 = (TextView) findViewById(R.id.textview4);

 Resources res = getResources();
 String[] steps = res.getStringArray(R.array.dance_steps);

 step1.setText(item number 1 in String Array);
 step2.setText(item number 2 in String Array);
 step3.setText(item number 3 in String Array);
 step4.setText(item number 4 in String Array);

Thankyou.

devRey
  • 53
  • 1
  • 8

3 Answers3

0
step1.setText(steps[0]);
step2.setText(steps[1]);
step3.setText(steps[2]);
step4.setText(steps[3]);

You should go through Java basics

Raaf003
  • 103
  • 1
  • 7
0

You just have to pass the string array. Here is more efficient code -

TextView step[] = new TextView[4];
step[0] = (TextView) findViewById(R.id.textview1);
step[1] = (TextView) findViewById(R.id.textview2);
step[2] = (TextView) findViewById(R.id.textview3);
step[3] = (TextView) findViewById(R.id.textview4);

Resources res = getResources();
String[] steps = res.getStringArray(R.array.dance_steps);

for(int i = 0 ; i < 4 ; i++){
    step[i].setText(steps[i]);
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
Shunan
  • 3,165
  • 6
  • 28
  • 48
0
TextView step[] = new TextView[4];
step[0] = (TextView) findViewById(R.id.textview1);
step[1] = (TextView) findViewById(R.id.textview2);
step[2] = (TextView) findViewById(R.id.textview3);
step[3] = (TextView) findViewById(R.id.textview4);

Resources res = getResources();
String[] steps = res.getStringArray(R.array.dance_steps);

for(int i = 0 ; i < steps.length ; i++){
    step[i].setText(steps[i]);
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27