jeudi 12 février 2015

How to code a function that randomly selects a quote from an array? - Android Java

I am following an android coding tutorial and am try to make the quote selection random. Below is the unaltered code.



final ArrayList<Quote> quoteList = new ArrayList<Quote>();

Quote quote1 = new Quote("Would I rather be feared or loved? Easy. Both. I want people to fear how much they love me.", "Michael Scott");
quoteList.add(quote1);

Quote quote2 = new Quote("I'm not superstitious. But I am a little stitious.", "Michael Scott");
quoteList.add(quote2);

Quote quote3 = new Quote("I like waking up to the smell of bacon.", "Michael Scott");
quoteList.add(quote3);

Quote quote4 = new Quote("Wikipedia is the best thing ever. Anyone in the world can write anything they want about any subject. So you know you are getting the best possible information.", "Michael Scott");
quoteList.add(quote4);

Quote quote5 = new Quote("Mo' money, mo' problems.", "Michael Scott");
quoteList.add(quote5);

Quote quote6 = new Quote("SWAG! Stuff We All Get.", "Michael Scott");
quoteList.add(quote6);

Quote quote7 = new Quote("You just gots to get your freak on.", "Michael Scott");
quoteList.add(quote7);

Quote quote8 = new Quote("We're all homos. Homo sapiens.", "Michael Scott");
quoteList.add(quote8);

Quote quote9 = new Quote("Hate to see you leave but love to watch you go. 'Cause of your butt.", "Michael Scott");
quoteList.add(quote9);

Quote quote10 = new Quote("If a baby were president, there would be no taxes, there would be no war.", "Michael Scott");
quoteList.add(quote10);
//Add more quotes here


touch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (count < quoteList.size()) {
Quote q = quoteList.get(count);
quoteText.setText(q.getQuote());
personText.setText(q.getPerson());
count = count + 1;
} else{
count = 0;
}
}
});
}


If anyone could explain how to make the app randomly select a quotew when the screen is touched, instead og going through them one by one, I would really appreciate it.





Aucun commentaire:

Enregistrer un commentaire