I have some problem trying to figure out this: I have a set of instances of an abstract class (Exercise) which handles single exercises, and a set of instances of an other class (Traning Class) that contains the specific exercises.
My app choose a random training class, and then runs a random exercise from it. From the settings, I’d like to be able to choose for example which training classes and which exercise to use from random selection
Here is my code
/** Common interface for all exercises */
public interface Exercise {
public Exercise run();
}
public abstract class ExerciseClass implements Exercise {
private int mWaitingTime = 3; //seconds to wait before answer is shown
private String mQuestion = "";
private String mAnswer = "";
private String mHint = "";
/*Getters and setters follow*/
}
This is an example of specific Training Class, where the exercises are added
public class MatheMagic extends TrainingClass {
public MatheMagic() {
class TwoDigitsX11 extends ExerciseClass {
public ExerciseClass run() {
String[] aRes = new String[3];
/*Choose a two digit number*/
int aRand = RandInt(100,11);
String aQuestion = aRand + " x 11";
String aAnswer = String.valueOf(aRand * 11);
String aHint = "To multiply by 11, take the first digit and the last digit, and put in the middle the sum of the two. I.E. 36 x 11 = 3 (3+6) 6 -> 396";
this.setQuestion(aQuestion);
this.setAnswer(aAnswer);
this.setHint(aHint);
return this;
}
}
//Set specific waiting times
TwoDigitsX11 aTwoDigitsX11 = new TwoDigitsX11();
aTwoDigitsX11.setWaitingTime(5);
//Add exercises to training class
mExerciseTypes.add(aTwoDigitsX11);
//these are other examples of exercises, whose code I’ve now not included
mExerciseTypes.add(aMultiplicationTables);
mExerciseTypes.add(new SquareTwoDigitsEndingFive ());
}
}
Now, in my main activity, I have:
private ArrayList<TrainingClass> mTrainingClasses ;
mMathMag = new MatheMagic();
mMnemonics = new Mnemonics();
mTrainingClasses = new ArrayList<TrainingClass>();
mTrainingClasses.add(mMathMag);
mTrainingClasses.add(mMnemonics);
Then, as I said, I have a function runRandomExercise, which selects a random element from mTrainingClasses and then a random element from the ExerciseClass array list within it
From my settings, I want to be able to
1) change dynamically the ArrayList (for example, telle that I want to select from mMnemonics but not mMathMag)
2) Select which exercise chose from in the specific TraningClass. Set if, for example, mMathMag can select the exercise type TwoDigitsX11
3) Change the waiting time for the specific exercise (accessing to the function setWaitingTime() )
My problem is that I can’t make a set of specific variable to handle this, because I want to be able to add or delete specific training classes and exercises, so ideally the app should be able to access from the setting page the mTrainingClasses element and handle it.
How can this be done?
Thanks!
This question is rather extensive to answer, so here goes only the answer to how to read from preferences:
You do not really need to use android settings for this. You may use an activity holding the user’s choices and returning the result to the activity with the exercises. You would need to implement the data persitance (dB) however if you want to keep those. It is a matter of design
To do it with android’s settings, consider this main activity:
Just notice how you can call the settings activity and how you can read its values, in this case it is checkbox1 (a boolean) in onCreate. To be able to resolve such intent you would need to have the settings activity declared in the manifest:
The preferences activity would look like:
And finally , and most important your preferences xml will have to be categorized, like this:
You can hold more than booleans, just read android’s guide regarding preferences. Note that you can categorize here the elements so you can have MatheMagic, mnemonics and all your stuff as categories.
This is the whole code by the way.. as this answer is showing how to use android preferences so you can just test it before attaching it to your app. Finally I would consider the following regarind preferences:
The deprecation is there because you “should” use fragments, but try first without.
From here, just read in your mainactivity the preferences (onCreate) and dynamically generate the exercises..
Hope it helps