Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:45:21+00:00 2026-05-23T12:45:21+00:00

I have used this question here on stackoverflow to create a random string without

  • 0

I have used this question here on stackoverflow to create a random string without problem 😀
( 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:45:21+00:00Added an answer on May 23, 2026 at 12:45 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string representing a path. Because this application is used on Windows,
I have copied this question here from spring forum . I have a parent
Here's a string that I may have: (MyStringIsOneWholeWord *) I have used the following
I have seen this question asked a few times but I have not seen
I have used this in my HTML: <q> Hai How r u </q> Which
I have a Status class in C#, used like this: Status MyFunction() { if(...)
I have heard this concept used frequently, but I don't have a really good
In our code we used to have something like this: *(controller->bigstruct) = ( struct
In the past, I have not really used namespaces, but in this project I
I have this code inside a class that is used by an application and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.