Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:58:04+00:00 2026-06-13T09:58:04+00:00

I have some problem trying to figure out this: I have a set of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:58:05+00:00Added an answer on June 13, 2026 at 9:58 am

    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:

    public class MainActivity extends FragmentActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView myText = new TextView(this);
            SharedPreferences mySharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    
            String text = ((Boolean)mySharedPref.getBoolean("checkbox1", true)).toString()+" <- checkbox 1";
            myText.setText(text);
            setContentView(myText);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            menu.add(0, 0,0, R.string.hello_world); 
            return true;
        }
    
        @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
    
            Intent i = new Intent("com.example.testdialogfragment.SettingsActivity");
            startActivity(i); // <------ Call settings as an activity
            return super.onMenuItemSelected(featureId, item);   
        }
    
    }
    

    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:

        //.... all the manifest..    
        <activity
                    android:name=".SettingsActivity" 
                    android:label="settings" 
                    android:theme="@android:style/Theme.Black">
                    <intent-filter>
                        <action android:name="com.example.testdialogfragment.SettingsActivity" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
        </activity>
    

    The preferences activity would look like:

    public class SettingsActivity extends PreferenceActivity {
    
        @SuppressWarnings("deprecation")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
    
        }
    
    }
    

    And finally , and most important your preferences xml will have to be categorized, like this:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
         <PreferenceCategory 
            android:title="Exercise Category1"
            android:key="pref_key_storage_settings">
            <CheckBoxPreference
                android:key="checkbox1"
                android:title="checkbox1"
                android:summary="summary"
                android:defaultValue="true" />
            <CheckBoxPreference
                android:key="checkbox2"
                android:title="checkbox1"
                android:summary="summary"
                android:defaultValue="true" />
        </PreferenceCategory>
        <PreferenceCategory 
            android:title="Exercise Category2"
            android:key="pref_key_storage_settings">
            <CheckBoxPreference
                android:key="checkbox3"
                android:title="checkbox3"
                android:summary="summary"
                android:defaultValue="true" />
        </PreferenceCategory>
    
    </PreferenceScreen>
    

    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:

    • What view is easier to customize a preference or a layout for an activity?
    • If you want to put actual preferences to your app like: “Enable sounds” you will have to pack them all together. Read the diagram posted here

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query, which is giving me some problems... I am trying to
i've been trying to figure this out and i have absolutely no clue why
I need to abstract some behavioural code and have a problem trying to reference
I've spent the day trying to figure out a strange problem. I have a
I am trying to figure out following problem. I have a simple rich:select <rich:select
I have some problem in my JDBC code. I am trying to connect through
I have a problem trying to design some generic storage.. Basically I have the
I'm trying to get a regexp to solve the next problem: I have some
Gday All, I have a baffling problem whilst trying to insert some chinese characters
I have some problem with the cursor when using JOprionPane. I set a cursor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.