Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Glen Hayes
5,798 PointsAndroid App
Hi there,
How there, if I have code like this:
mChestExerciseTenSets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Class[] classes = new Class[4];
classes[0] =FlatBenchPress.class;
classes[1] = CloseGripDumbellPress.class;
classes[2] = InclineBenchPress.class;
classes[3] = ReverseDeclinesStanding.class;
Random r = new Random();
Intent i = new Intent(ChestBackExercises.this, classes[r.nextInt(classes.length)]);
startActivity(i);
}
});
which goes to a random class with a button click, how would I go about only going to a randomly selected class only once and then removing that class from the array so it cant be selected again? Also, if I have several buttons on one activity how do I keep track and also store what button is pressed?
Thanks in advance!
3 Answers
Steve Hunter
57,712 PointsHi Glen,
If you want to modify the array, you don't want to use an array - they are immutable so you can't change their length once initialized. I suggest trying an ArrayList. That gives you the ability to flex the length of the list.
As the new Activity ends, it should return to the line after startActivity(i), I think - try that to make sure! At that point, the variables i and r should still be in scope and hold the values they did before. So, you can delete the classes[r] at that point.
I must say that having all that heavy-lifting done in the onClick method doesn't look like the way forward to me.
It'll be very slow and error-prone. There must be a better way of achieving what you're attempting to do without needing to load up the onClick method like that.
Steve.
Glen Hayes
5,798 PointsHey Steve, thanks for the reply. As I am very much a beginner, the onclick method for me is the only real way I currently know how to achieve what I want done.
I will give the ArrayList a go. Not really following 100% what you mean by returning to the line after startActivity(i), but I will have a crack!
Thanks again.
Steve Hunter
57,712 PointsTrial and error is a great way to get to know your code!
Maybe generate your arrays inside the onCreate() method or call a separate method there that does the heavy work?
The bit about returning to the line - you called the startActivity(i) method and that goes off and does stuff defined in your new activity. When you close that activity, the code execution can/may/perhaps should return to where it was called from. So, if there was code after the startActivity() line, that would then execute.
Good luck!
Steve.