String array list display problem

試著忘記壹切 提交于 2020-01-03 04:34:35

问题


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:


Do like this for next

btnNext.setOnClickListener(new OnClickListener() {

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



        }

               });



回答2:


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.



来源:https://stackoverflow.com/questions/6937872/string-array-list-display-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!