Although this is terrible, I’ll still show a possible way to do this. But just remember, this is TERRIBLE.
You can store all the activity classes in an ArrayList
.
ArrayList<Class<Activity>> activities = new ArrayList<> ();
And then you add all the activities into the ArrayList
. Yeah, I know, this part is tedious. For example,
activities.add (Activity1.class);
And then you create a Random
called rand
and use that to access an element in the list:
list.get (rand.nextInt (list.size()));
Here is another “better” way to do it, but it’s still kinda bad. I strongly advise you to store teh questions in a database. Anyway, here’s the better-but-still-bad method.
You create a question class:
public class Question {
//here you can put correctAnswer, questionText etc
}
After that, you make an ArrayList
of Question
s i.e. ArrayList<Question>
.
ArrayList<Question> questions = new ArrayList<> ();
And still, you need to add 1000 questions to the array list. Then you can just use a Random
to access it.
When you want to display a question in one activity, you can just putExtra
in Intent
before starting the activity. putExtra
basically passes some “parameter” thingys to the activity. Then in the activity, you can just get the “Extra” and use the Question
object to display. e.g. set the text of a TextView
to the question text.
4
solved I have around 1000 different activities in my Android App. How can I jump to a random activity?