android - Can't display one string from array on button press -


i'm new arrays in java , our final project, instead of creating 3000 activities, decided use single array house strings. problem i'm having when press button change string on screen, either skips end or adds somehow. want show 1 string @ time , cannot, life of me figure out.

here code:

public class mainactivity extends activity {      mediaplayer snake;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         final string[] lines = {"so begins story of our hero.","his name solid snake.","he international spy, elite solider, , quite ladies man.",                 "snake likes sneak around in cardboard box.","most enemies aren't smart enough catch him in it."};         snake = mediaplayer.create(this, r.raw.maintheme);         snake.start();         final textview tv = (textview)findviewbyid(r.id.textview1);         button n = (button)findviewbyid(r.id.next);         button b = (button)findviewbyid(r.id.back);         int count = 0;         tv.settext(lines[count]);          n.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 string temp = "";                 for(int l=1; l<lines.length; l++){                     temp=temp+lines[l];                     tv.settext(""+temp);                 }              }              });         }; 

the main problem in button press. i've searched everywhere , couldn't find answer @ all. appreciated.

when button clicked text changed through each entry in array, finishing on last one. since happens see last value.

your onclick() method should changed call settext() once , increment counter held in activity.

public class mainactivity extends activity {     private int currentline = 0;     ...     @override     protected void oncreate(bundle savedinstancestate) {         ...         tv.settext(lines[currentline]);          n.setonclicklistener(new onclicklistener() {             @override             public void onclick(view v) {                 if (currentline + 1 < lines.length) {                     currentline++;                     tv.settext(lines[currentline]);                 }             }         });         ...     }     ... } 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -