devRey devRey - 4 months ago 6
Java Question

How to get an item in string array and put it on different TextView

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.

Answer

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]);
}
Comments