android - Hide/remove random string? -


i have used question here on stackoverflow create random string without problem :d ( show random string)

some times same string shows , that's annoying.

so want string show 1 time per session, have quit session button kills class. lets have numbers 1-3. number 2 shows first, 1, becuase there's 1 number left 3 can shown.

my button code "next button". kills class , starts again! how can change displays new string?

private void onbuttonclick(button clickedbutton) {     intent startintent = null;     if (clickedbutton.getid() == r.id.quit) {         startintent = new intent(this, mainmenu.class);         finish();     }      else if (clickedbutton.getid() == r.id.next) {          startintent = new intent(this, play.class);          startactivity(startintent);           finish();      }      if (startintent != null) {         startintent.setflags(intent.flag_activity_reorder_to_front);         startactivityifneeded(startintent, 0);     } } private void setupbutton() {             view.onclicklistener onclickhandler = new view.onclicklistener() {                    public void onclick(view v) {             onbuttonclick((button)v);                        }     };     button button = (button)findviewbyid(r.id.quit);     button.setonclicklistener(onclickhandler);      button = (button)findviewbyid(r.id.next);     button.setonclicklistener(onclickhandler); 

the same string appearing because random number generator generates random numbers! generate same number multiple times in row.

there many possibilities, name two:

  1. put strings in array , shuffle it. take elements out 1 one starting index one:

    list strings = new arraylist<string>(getresources().getstringarray(r.array.myarray)); collections.shuffle(list); 
  2. put strings in array, select string random index , remove string:

        random rgenerator = new random();      arraylist<string> strings = new arraylist<string>(getresources().getstringarray(r.array.myarray));                      int index = rgenerator.nextint(strings.size());     string randomstring =  strings.get(index);     strings.remove(index); 

edit: ok, see...

you have remember strings have not been used already. store strings in list , make list static, can save state between creation of different instances of activityplay:

private static arraylist<string> mystring; 

then change constructor bit:

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.play);     setupbutton();      if (mystring == null || mystring.size() == 0) {         resources res = getresources();         mystring = new arraylist(arrays.aslist(res.getstringarray(r.array.myarray)));     }      // random string     int rand = rgenerator.nextint(mystring.size());     string q = mystring.get(rand);      // remove last used string list     mystring.remove(rand);      textview tv = (textview) findviewbyid(r.id.text1);     tv.settext(q); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -