0

I like to display some strings from a string array in to a Textview, randomly. I am using the following code for that, But the problem is that most of the time some string is displayed repeatedly.What I want is the strings once displayed should not be displayed again.I have spent hours in searching for the code, but none of them work for me. Kindly help. Thanks in advance.

public void GetQuotes(View view) {
     Resources res = getResources();
            myString = res.getStringArray(R.array.Array);
            String q = myString[rgenerator.nextInt(myString.length)];                               
            TextView tv = (TextView)findViewById(R.id.textView1);               
            tv.setText(q);  
2
  • Ah, good ol' pseudorandom. How about this? Create two lists of strings. Use your random on list1 to extract a string. Remove it from list1 and put it in list2. Your next random will definitely not be the same as last one. When you are done extracting the last string from list1, put all the strings from list2 back in list one and empty list2 and start over again? Commented Jan 22, 2013 at 7:53
  • I don't know how to do that. Can you suggest a less complicated method Commented Jan 22, 2013 at 8:14

4 Answers 4

1

Java has built in array shuffling method, put all your items into a list, shuffle it randomly, and get the first element till it has elements. If empty, add all elements again, and shuffle again:

private List<String> myString;

public void GetQuotes(View view) {
    Resources res = getResources();
    if (myString==null || myString.size()==0) {
        myString = new ArrayList<String>();
        Collections.addAll(myString, res.getStringArray(R.array.Array));
        Collections.shuffle(myString); //randomize the list
    }
    String q = myString.remove(0);
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(q);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'd suggest either checking manually if it's been used before or use a set and then write the strings in that set.

http://developer.android.com/reference/java/util/Set.html

2 Comments

If you want code hire a programmer. Gauravs answer shows you how to create a set. There's bound to be lots of tutorials on how to use a set if you look.
Thanks. I am a starter in Android and learning things. I will try set.
0

Arrays and Lists in general aren't designed to avoid duplicates, they're designed as a type of collection that maintains the order of a number of elements. If you want a collection more suited for the job then you want a set:

 Set<String> set = new HashSet<String>();

to avoid duplicates.

Comments

0

Here is a very simple solution.

Now while I am saying this tongue in cheek, if you want a simple solution, you can have a dedicated string variable that stores the last used question. Then it becomes very simple if you initialized it as an empty string. Say the variable is called last.

String q = myString[rgenerator.nextInt(myString.length)]; 
//q has a string which may or may not be the same as the last one
//the loop will go on until this q is different than the last
//and it will not execute at all if q and last are already different
while (last.equals(q))
{
    //since q and last are the same, find another string
    String q = myString[rgenerator.nextInt(myString.length)]; 
};
//this q becomes last for the next time around
last = q;

Now among a few other issues, a key thing to remember here is that this only makes sure that q[1] cannot follow q[1] but it does not entirely avoid a scenario of, just to be ridiculous, say q[1], q[2], q[1], q[2] and so on.

Here is one with ArrayList which is equally simple.

List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
for (int i = 0; i < myString.length)
{
    list1.add(myString[i]);
}
q = (String)list1.get(rgenerator.nextInt(list1.size()));
list1.remove(q);
list2.add(q);
if (list1.isEmpty())
{
    list1.addAll(list2);
    list2.clear();
}

2 Comments

My Emulator was crashing frequently in Win7. So I have downloaded it for Ubuntu. Now its fine. I have tried your sol, there were no errors but when running, the app crashes. But fortunately Daniel Fekete's solution worked for me. Thank you so much
+1 for linux. Also, im glad that his solutuon worked for you. Please select his answer then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.