0

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.

1
  • just see are you setting clickListener for exact button or not.... Commented Aug 4, 2011 at 7:59

2 Answers 2

1

Do like this for next

btnNext.setOnClickListener(new OnClickListener() {

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



        }

               });
Sign up to request clarification or add additional context in comments.

5 Comments

item=item%words.length(); in this line what is the meaning for %? it does not work right now?
when item will be greater or equal to length you will get array index out of bound exception.So you just changes it to 0 when it reaches to the length of the word array
No, it run successfully of answer of Egor . Only show up error when i clicked last word and sentence(last Next). Do you have any idea, return back to initial position(it means "the" in word and "the little boy" in sentence of array when i am click last next.
yes i told you that.Your words array have length and you can't access when it exceeds the length.Because you are accessing the next index at each click once it exceeds it size.So you have do something so that it works.
So i made a remainder .whenever it exceeds the highest index it will be 0
1

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.

4 Comments

But one things...you see index text tag...i am also want to index if i am "is" in word and "is about me"in sent which is 3 of 5 into the index. so what i do for this.
another problem faced at the last time i click next button, error was occurred??
@Kshetri Horrorgoogle, if your item == words.length, you should call item = 0
,i try this for increment the index position on left side.public void onClick(View v) { item+=1; tw.setText(words[item]); ts.setText(sent[item]); tIndex.setText(item+"of"+"25"); /*if(item==words.length || item==sent.length){ item=0; }*/ } }); But can not work. what is the error of this?

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.