android - Displaying the first element of the array/string in the textview and starting the array from it -


i have code in moving text in forward , reverse direction of next , button. problem facing need start displaying first element in string , moving string forward , reverse element. here code

    string[] answers = { "red", "green", "blue" };     private int _position = answers.length - 1;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.learn_main);          button btnnext = (button) findviewbyid(r.id.button2);         button btnback = (button) findviewbyid(r.id.button1);          final textview tv = (textview) findviewbyid(r.id.textview1);         tv.settext(answers[_position]);            // initializing on         btnnext.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view arg0) {                   if (_position < answers.length - 1) {                     _position++;                 } else {                     _position = 0;                 }                 tv.settext(answers[_position]);              }          });          btnback.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view arg0) {                  if (_position > 0) {                     _position--;                 } else {                     _position = answers.length - 1;                 }                 tv.settext(answers[_position]);              }          });      } } 

i have tried assigning value want start in xml, doest because first element repeating twice.

the following didn't too

tv.settext(answers[0]); 

your logic right, little wrong. try modified one. hope work.

string[] answers = { "red", "green", "blue" }; private int _position = 0;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.learn_main);      button btnnext = (button) findviewbyid(r.id.button2);     button btnback = (button) findviewbyid(r.id.button1);      final textview tv = (textview) findviewbyid(r.id.textview1);     tv.settext(answers[_position]);        // initializing on     btnnext.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {               if (_position < answers.length - 1) {                 _position++;             } else {                 _position = 0;             }             tv.settext(answers[_position]);          }      });      btnback.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {              if (_position > 0) {                 _position--;             } else {                 _position = answers.length - 1;             }             tv.settext(answers[_position]);          }      });  } 

}


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -