|
@@ -3,6 +3,7 @@ package org.uab.android.persistentdata.sharedpreferences;
|
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
import android.app.Activity;
|
|
|
|
|
+import android.content.SharedPreferences;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
import android.view.View.OnClickListener;
|
|
import android.view.View.OnClickListener;
|
|
@@ -18,16 +19,23 @@ public class MainActivity extends Activity {
|
|
|
TextView currentScoreTextView;
|
|
TextView currentScoreTextView;
|
|
|
Button playButton;
|
|
Button playButton;
|
|
|
Button resetButton;
|
|
Button resetButton;
|
|
|
|
|
+
|
|
|
|
|
+ SharedPreferences gameScoreSharedPrefs;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_main);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
|
|
+ // Get a reference to the SharedPreferences file
|
|
|
|
|
+ gameScoreSharedPrefs = getPreferences(MODE_PRIVATE);
|
|
|
|
|
+
|
|
|
// Get a reference to the high score TextView
|
|
// Get a reference to the high score TextView
|
|
|
highScoreTextView = (TextView) findViewById(R.id.highScoreTv);
|
|
highScoreTextView = (TextView) findViewById(R.id.highScoreTv);
|
|
|
|
|
|
|
|
//TODO Update the high score based on the saved high score
|
|
//TODO Update the high score based on the saved high score
|
|
|
|
|
+ int highScore = gameScoreSharedPrefs.getInt(HIGH_SCORE_KEY, 0);
|
|
|
|
|
+ highScoreTextView.setText(String.valueOf(highScore));
|
|
|
|
|
|
|
|
// Get a reference to the current game score TextView
|
|
// Get a reference to the current game score TextView
|
|
|
currentScoreTextView = (TextView) findViewById(R.id.currentScoreTv);
|
|
currentScoreTextView = (TextView) findViewById(R.id.currentScoreTv);
|
|
@@ -47,6 +55,19 @@ public class MainActivity extends Activity {
|
|
|
|
|
|
|
|
//TODO Check whether the current score is greater than the high score
|
|
//TODO Check whether the current score is greater than the high score
|
|
|
// and update the high score accordingly
|
|
// and update the high score accordingly
|
|
|
|
|
+
|
|
|
|
|
+ // Get store high score
|
|
|
|
|
+ int storedScore = gameScoreSharedPrefs.getInt(HIGH_SCORE_KEY, 0);
|
|
|
|
|
+ if ( value > storedScore ) {
|
|
|
|
|
+
|
|
|
|
|
+ // Set and edit high score
|
|
|
|
|
+ SharedPreferences.Editor editor = gameScoreSharedPrefs.edit();
|
|
|
|
|
+ editor.putInt(HIGH_SCORE_KEY, value);
|
|
|
|
|
+ editor.commit();
|
|
|
|
|
+
|
|
|
|
|
+ // Update the high score TextView
|
|
|
|
|
+ highScoreTextView.setText(String.valueOf(value));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -57,6 +78,12 @@ public class MainActivity extends Activity {
|
|
|
public void onClick(View v) {
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
// TODO Set current game and high score to 0
|
|
// TODO Set current game and high score to 0
|
|
|
|
|
+ SharedPreferences.Editor editor = gameScoreSharedPrefs.edit();
|
|
|
|
|
+ editor.putInt(HIGH_SCORE_KEY, 0);
|
|
|
|
|
+ editor.commit();
|
|
|
|
|
+
|
|
|
|
|
+ highScoreTextView.setText(String.valueOf(0));
|
|
|
|
|
+ currentScoreTextView.setText(String.valueOf(0));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|