4

I have a list of string arrays:

<string-array name="chapter_1">
      <item>......</item>
      .           
      .
      .
      .
</string-array>

<string-array name="chapter_2">
      <item>......</item>
      .           
      .
      .
      .
</string-array>

and so on. I have got a random int chapter_no.

I have a string:

String chapter_name = "chapter_" + chapter_no;

Now, I want to access string array corresponding to the chapter_no.

I know that I can't do like this:

String[] chapter = getResources().getStringArray(R.array.chapter_name);

How can I access the string arrays randomly? Please help me.

9
  • Do you want a random String Array or a random String from a known chapter? Commented Jun 10, 2015 at 12:27
  • First I want to get string array and then a string from chapter. Let me say I want a random string ( item ) from chapter. Commented Jun 10, 2015 at 12:29
  • Do u want random string(item) from any specific chapter or from all? Commented Jun 10, 2015 at 12:31
  • @learner can 't u iterate through the element and find the element you are looking for? Commented Jun 10, 2015 at 12:32
  • 1
    If I understand you correctly, then @Moinkhan 's answer should help you achieve what you want Commented Jun 10, 2015 at 12:38

5 Answers 5

2

To get Random String from chapter_array ..

String[] chapter = getResources().getStringArray(R.array.chapter_name);

String randomString = chapter[new Random(chapter.length).nextInt()];

[Updated]

        String[] data;

        //first of all give all resources to arrays
        int[] arrays = {R.string.chapter_1, R.string.chapter_2};

        //then we are fetching any one string array resources randomly
        int chapter_no = arrays[new Random(arrays.length).nextInt()];

        //From above we got any one random resource string..So we fetch all string items from that resource into data array
        data = getResources().getStringArray(chapter_no);

        //Now from that data array we fetch any one random string.
        String randomString = data[new Random(data.length).nextInt()];

[updated]

    String[] data;
    int[] arrays = {R.array.chapter_1, R.array.chapter_2};
    int chapter_no = arrays[new Random().nextInt(arrays.length-1)];
    data = getResources().getStringArray(chapter_no);
    String randomString = data[new Random().nextInt(data.length-1)];
    Toast.makeText(this, randomString, Toast.LENGTH_SHORT).show();
Sign up to request clarification or add additional context in comments.

9 Comments

I want to make sure one thing ,... the chapter_name is available to you or not ....???
chapter_name is a variable, not a constant.. Please go through question. It is not working at all and I tried it..
Yeah, I got a solution like this here: stackoverflow.com/questions/12139015/… But I don't want to make an array because there are 100 chapters.
But if you are talking about 100 chapters then you should have to Sqlite ... which is batter option for you..
I know that. but I am using string-array. And also using array instead of string works..
|
1

Once try as follows

UPDATE

int id = getResources().getIdentifier(chapter_name, "array",this.getPackageName()); 
String[] chapter = getResources().getStringArray(id);

Hope this will helps you.

2 Comments

Your answer is working well. thank you for the answer. I can't find any documentation for the topic. Please suggest me one.
@learner i am also new to this i referred following developer.android.com/reference/android/content/res/…
0

If you just have 2 strings and you want to get them randomly you can do it like this:

Random random = new Random();

int chapter_no = random.nextInt(2) + 1;
String chapter_name = "chapter_" + chapter_no;

This Random function that I put above will get values until number 2.

I expect it helps to you!

3 Comments

I have more than 2 string arrays and corresponding chapter no.
@learner and do you know exactly the number of string arrays that you have there?
@learner Then you can change the number inside the nextInt witht the number of string-arrays that you have (the total). random.nextInt(total) + 1;
0

Create an array of all those array ids, and then pick one of them in that array.

int[] arrays = {R.array.chapter_1,....};

Randomly pick one among these array.

 Random random = new Random();
 int chapter_no = arrays[random.nextInt(arrays.length)];

2 Comments

As described in the given question? Can't i have a better solution? stackoverflow.com/questions/12139015/…
but you have an array of string-array.
0

You Can Do this :

String[] chapter = getResources().getStringArray(R.array.chapter_1);
String[] chapter2 = getResources().getStringArray(R.array.chapter_2);
Random rand=new Random();
int randomNo=rand.nextInt();


ArrayList<String> temp = new ArrayList<String>();
temp.addAll(Arrays.asList(chapter));
temp.addAll(Arrays.asList(chapter2));
String [] concatedArgs = temp.toArray(new String[chapter.length+chapter2.length]);
String item=concatedArgs[randomNo];

2 Comments

I don't want this solution man. This will select from chapter_2. I want to get from any chapter.
please don't delete the previous answer.. some times other users get confused. and write edit on the top of the answer.. I will try your solution also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.