0

I have used this question here on stackoverflow to create a random string without problem :D ( Show random string)

Some times the same string shows up and that's annoying.

So I want the string to only show one time per session, I have already a Quit Session button that kills the class. So lets say I have numbers from 1-3. Number 2 shows up first, then 1, becuase there's only one number left only 3 can be shown.

My button code for the "next button". Currently it kills the class and starts it again! How can I change it so it just displays a 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);
Community
  • 1
  • 1
JeppeHP
  • 45
  • 1
  • 4

2 Answers2

0

Make a random permutation of the indexes and then use it to pick the next string in the sequence.

trutheality
  • 23,114
  • 6
  • 54
  • 68
0

The same string is appearing because the random number generator generates random numbers! It could generate the same number multiple times in a row.

There are many possibilities, to name two:

  1. Put all strings in an array and shuffle it. Then take the elements out one by one starting with index one:

    List strings = new ArrayList<String>(getResources().getStringArray(R.array.myArray));
    Collections.shuffle(list);
    
  2. Put all strings in an array, select a string with a random index and remove the 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, I see...

You have to remember which strings have not been used already. Store the strings in a List and make the list static, so you can save state between the creation of different instances of the Activityplay:

private static ArrayList<String> myString;

Then change your constructor a 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)));
    }

    // get a random string
    int rand = rgenerator.nextInt(myString.size());
    String q = myString.get(rand);

    // remove the last used string from the list
    myString.remove(rand);

    TextView tv = (TextView) findViewById(R.id.text1);
    tv.setText(q);
}
thaussma
  • 9,756
  • 5
  • 44
  • 46
  • doesn't work! getting errors. I have already a random generating thing, I added "String randomString = strings.get(index); strings.remove(index);" and changed the strings to MyString. – JeppeHP Jul 03 '11 at 10:24
  • Aha, so whats the error? What does your code look like. Do you mak any effort to help yourself? – thaussma Jul 03 '11 at 11:30
  • http://karlsson1337x.com/prtscr.jpg I removed the added code becuase I'm trying an other feature. And the code on the image is just a part of my class! Thanks. – JeppeHP Jul 03 '11 at 12:07
  • Just copy some code from above and use common sense to fit your code! Don't make screenshots from code, edit your question! – thaussma Jul 03 '11 at 12:14
  • Done. Please check my question again. – JeppeHP Sep 19 '11 at 11:21
  • Again I have edited the question :) Thanks for helping me out! – JeppeHP Sep 19 '11 at 20:49
  • I changed two lines. Try again. Actually you should get it to work yourself, if you have understood the concept... – thaussma Sep 20 '11 at 08:39
  • works but I still get the same strings over and over, I think it has to be with my "next" button. check the question for the button code. – JeppeHP Sep 20 '11 at 12:06