Reputation: 605
I am building an application that contains a quiz. As soon as the user finishes the quiz, an activity is shown to the user where it calculates his total score as a TextView
. What I want to achieve here is storing that TextView value shown to the user at the end of the quiz (containing the score), but also show that value store in another activity called where all the Highscores are showed. I believe I am storing the value, **but I cannot see it in my total scroe activity(not retrieving it)**This is initialized in both activities
private final static String storeHighscores="storeHighscores.txt";
Summary after quiz activity. the variable is called finalscore, int
finalScore = timeLeft * QuizActivity.correct;
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(storeHighscores, 0));
out.write(finalScore);
out.close();
Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
}
catch (Throwable t) {
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
TotaScore Activity
try {
InputStream in = openFileInput(storeHighscores);
if (in != null) {
InputStreamReader tmp=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) != null) {
buf.append(str);
}
in.close();
easy.setText(buf.toString());
}
}
Not sure if I am using the right approach here, please free to suggest me how to achieve what I want, or alternatives for this approach.
Upvotes: 1
Views: 61
Reputation: 43322
It seems that using SharedPreferences would be better suited for this task.
See this post: Saving high scores in Android game - Shared Preferences
Documentation: http://developer.android.com/reference/android/content/SharedPreferences.html
Upvotes: 1