I want to use a random number generator to pick one of the cards stored in my .xml file as a string array.
Currently i know i can change the text view by calling
String[] cards = getResources().getStringArray(R.array.card1);
text1.setText(cards[0]);
text2.setText(cards[1]);
text3.setText(cards[2]);
text4.setText(cards[3]);
text5.setText(cards[4]);
text6.setText(cards[5]);
and this will load the items in the string array correctly.
My question is when a random number is generated, how can i use that number to
.getStringArray(R.array.cardX);
where x is the integer generated so i can randomly generate a list of strings every time. I think i am looking to input some variable into .getSringArray(X) but i'm not sure how to do that. If you have any other suggestions on how to do this that would help as well. Thanks for your help
If I understand you correctly, you have several string array resources that you want to pick from at random.
Unfortunately, the resource IDs are generated into variables, and there is no way to generate a resource ID from a string. If you look at R.java
in your build directory, you will notice that the resource IDs are declared as actual variables in there.
The way you would have to do this is by creating a list of resource IDs to be selected from as so:
import java.util.Random;
int[] cardStringResourceIds = {
R.array.cards1,
R.array.cards2,
R.array.cards3,
};
Random random = new Random();
int randomIndex = random.nextInt(cardStringResourceIds.length);
String[] cards = getResources().getStringArray(cardStringResourceIds[randomIndex]);