Reputation: 35
I am trying to adapt this code http://automateddeveloper.blogspot.co.uk/2011/06/getting-started-complete-android-app.html for my quiz game. I have downloaded http://sourceforge.net/projects/sqlitebrowser/ as suggested in the comment section of the link above. I have my own db running on my device and I am using Eclipse and targeting Android 4. All works. What I cannot get to work is the SettingsActivity even though in my new db I have changed the difficulty ratings and made sure there are Easy, Medium and Hard.
When I leave the Settings on "Medium" difficulty the quiz game goes off without a hitch. When I change the Settings to "Easy" and then go back to menu and click play the game tells me it is stopping.
I am posting what I feel are the relevant code. I have not been able to find anything other than what is posted in the comment section of the link.
The SplashActivity:
private List<Question> getQuestionSetFromDb() throws Error {
int diff = getDifficultySettings();
Syste`enter code here`m.out.print("diff" + diff);
//Log("diff" + diff);
int numQuestions = getNumQuestions();
DBHelper myDbHelper = new DBHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
myDbHelper.close();
return questions;
}
/**
* Method to return the difficulty settings
* @return
*/
private int getDifficultySettings() {
SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
int diff = settings.getInt(Constants.DIFFICULTY, Constants.EASY);
return diff;
}
The SettingsActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
/**
* set listener on update button
*/
Button updateBtn = (Button) findViewById(R.id.nextBtn);
updateBtn.setOnClickListener(this);
/**
* Set selected button if saved
*/
updateButtonWithPreferences();
}
/**
* Method to update default check box
*/
private void updateButtonWithPreferences() {
RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);
SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
switch (diff)
{
case Constants.EASY :
c1.toggle();
break;
case Constants.MEDIUM :
c2.toggle();
break;
case Constants.EXTREME :
c3.toggle();
break;
}
}
@Override
public void onClick(View arg0) {
/**
* check which settings set and return to menu
*/
if (!checkSelected())
{
return;
}
else
{
SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
Editor e = settings.edit();
e.putInt(Constants.DIFFICULTY, getSelectedSetting());
e.commit();
finish();
}
}
/**
* Method to check that a checkbox is selected
*
* @return boolean
*/
private boolean checkSelected() {
RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);
return (c1.isChecked() || c2.isChecked() || c3.isChecked());
}
/**
* Get the selected setting
*/
private int getSelectedSetting() {
RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
if (c1.isChecked())
{
return Constants.EASY;
}
if (c2.isChecked())
{
return Constants.MEDIUM;
}
return Constants.EXTREME;
}
}
The rest of the code works fine include the new questions that I put into the db so I will keep the code posting at this unless someone request anything more.
Thanks a lot in advance, Terry
Upvotes: 0
Views: 82
Reputation: 7800
Use this to use application scope shared preferences:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Context context)
In place of: SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
Upvotes: 1