user4685238
user4685238

Reputation:

Show text according to Random Textview

I am showing Random text in the two textview's on button click. Now I have to show the explaination of the text showed in textview1 in textview2.But I am not able to get it. Help will be appreciated.

I put the text in string.xml

<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>

<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>

My code in MainActivity:

    Random number,number_explaination;
    int [] array_number,array_number_explaination;
    int textview_number,textview_number_explaination;
    TextView textView1,textView2;

    number = new Random();
    array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
            R.string.eight,R.string.nine,R.string.ten};
    textview_number = number.nextInt(array_number.length - 1);
    textView1.setText(array_number[textview_number]);

            number_explaination = new Random();
    array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
            R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
            R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
    textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
    textView2.setText(array_number_explaination[textview_number_explaination]);

What I want here if I get Random R.string.two in my textview1 and then I will get R.string.two_explaination and so on.How can I achive this. Sorry for my bad english.

Upvotes: 1

Views: 68

Answers (3)

Bhargav Thanki
Bhargav Thanki

Reputation: 4954

try this

    Random number,number_explaination;
    int [] array_number,array_number_explaination;
    int textview_number,textview_number_explaination;
    TextView textView1,textView2;

    number = new Random();
    array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
            R.string.eight,R.string.nine,R.string.ten};

    array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
            R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
            R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};  

    textview_number = number.nextInt(array_number.length - 1);
    textView1.setText(getResources().getString(array_number[textview_number]));
    textView2.setText(getResources().getString(array_number_explaination[textview_number]));

You do not need to generate another random number for explanation text

Upvotes: 1

Parth Anjaria
Parth Anjaria

Reputation: 3971

you need to use string array in the strings file :

  <string-array
        name="string_array_name">
        <item
            >text_string</item>
    </string-array>

and then make an array in your code with reference of :

R.array.string_array_name

see http://developer.android.com/guide/topics/resources/string-resource.html for reference.

after you got the two arrays in your code you can run loops and do your desired work

Upvotes: 0

longwalker
longwalker

Reputation: 1664

Try this:

textView2.setText(getString(array_number_explaination[textview_number_explaination]));

getString(int) should be used to get a string in your string resource file.

Upvotes: 1

Related Questions