Horrorgoogle
Horrorgoogle

Reputation: 7868

String array list display problem

I have create to String array one is "word" another is "sentence".

   <resources>
<string-array name="word">
         <item> the</item>
         <item> a</item>
         <item> is</item>
         <item> you</item>
         <item> to</item>
      </string-array>
 <string-array name="sentence">
         <item> the little boy</item>
         <item> a good boy</item>
         <item> is about me</item>
         <item> then you give</item>
         <item> was to come</item>
    </string-array>
   </resources>

Now i am trying access these two string array from the java code. which is

    final String  words []=getResources().getStringArray(R.array.word);
    final TextView tw=(TextView)findViewById(R.id.txtWord);
    tw.setText(words [item]);


    final   String []sent=getResources().getStringArray(R.array.sentence);
    TextView ts=(TextView)findViewById(R.id.txtSen);
    ts.setText(sent[item]);

    Button btnNext=(Button)findViewById(R.id.btnright);
    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            for(item=0;item<words.length;item++){
            tw.setText(words [item]);


            for(item=0;item<sent.length;item++){
            tw.setText(sent[item]);}
                               }
        }

               });
           }
     }

Iniailly Word is place to display words array and Sentence is place to display sentence array so dont be confuse. Here my intention is to display all above five item one item at a time only change word and sentence simultaneously if i click the next(>>) button above figure. but only word[0] and sent[0] was display. first time but cant not display if click >> button, which it turn to display "a" and "a good boy" respective position. Do you Have any idea behind this problem? Also want to change index from the bottom|left corner index. if i click next word and sentence.

Upvotes: 0

Views: 1233

Answers (2)

Rasel
Rasel

Reputation: 15477

Do like this for next

btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            item++;
            item=item%words.length(); 
            tw.setText(words [item]);



        }

               });

Upvotes: 1

Egor
Egor

Reputation: 40193

You should not start a loop on the button's click, instead you should just increment your item variable:

@Override
        public void onClick(View v) {

            item += 1;
            tw.setText(words [item]);
            ts.setText(sent[item]);

        }

This should work.

Upvotes: 1

Related Questions