Reputation: 115
I am doing a quiz
, in that I have 3 activities Question1
, Question2
, Question3
,and on each there is 4 possible answers (buttons), only one button is correct. I must calculate and display the score of the user at the end of the quiz.
I have done some research and SHAREDPREFERENECES
seem to be the solution. But i dont know how to use it can anyone help please. Question1.java
code is below: Assume that btnAnswer1a
is the correct answer, how do i save the data and then display it in a textview
in the last activity.
Button Answer1, Answer2, Answer3, Answer4;
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences settings;
SharedPreferences.Editor editor;
int ScoreCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.answer1);
settings = getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
ScoreCount = settings.getInt("ScoreCount", 10);
Answer1 = (Button) findViewById(R.id.btnAnswer1a);
Answer2 = (Button) findViewById(R.id.btnAnswer1b);
Answer3 = (Button) findViewById(R.id.btnAnswer1c);
Answer4 = (Button) findViewById(R.id.btnAnswer1d);
Answer1.setOnClickListener(this);
Answer2.setOnClickListener(this);
Answer3.setOnClickListener(this);
Answer4.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.btnAnswer1a:
Intent Screen1 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen1);
editor.putInt("ScoreCount", 6);
editor.commit();
break;
case R.id.btnAnswer1b:
Intent Screen = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen);
break;
case R.id.btnAnswer1c:
Intent Screen3 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen3);
break;
case R.id.btnAnswer1d:
Intent Screen2 = new Intent(Answer1.this, Color2.class);
Answer1.this.startActivity(Screen2);
break;
}
Upvotes: 1
Views: 4853
Reputation: 5212
Is there a particular reason why you'd want a different activity for each question? It seems pretty inefficient to me (redundant code, error prone, memory wise). You could store all questions and answers inside arrays and keep track of the current question and score using ints. When a question is answered, check the answer, update the score, increment the current question, fetch the new question and answers and set those to the question textfield and button labels. No need for passing the score that way as everything remains inside one activity.
Upvotes: 0
Reputation: 3846
I think it would be better if you pass the information with the go in intent. You can do so by:
Intent i = new Intent("com.yourPackage.yourActivity");
i.putExtra("keyForExtra", _ID);
startActivity(i);
and onCreate of the resultant activity you can
String data = getIntent().getStringExtra("keyForExtra");
You can parse this string to integer or whatever you feel like.
However if you are to do it with shared preferences, then, for writing:
SharedPreferences settings = getSharedPreferences(YOUR_CUSTOM_PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("myScore", yourScore); // you can put a lot of thngs there.
editor.commit();
To find out what else you can put look here
SharedPreferences settings = getSharedPreferences(YOUR_CUSTOM_PREFS_NAME, 0);
int currentScore = settings.getInt("myScore", defaultValue);
For detailed storage documentation of shared prefs look here
Upvotes: 1
Reputation: 399
When you want to write something to SharedPreferences use this
SharedPreferences sharedPreferences = getSharedPreferences("your_name_of_shared_preferences", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.putInt("result", resultValue);
editor.commit();
And to read something from them just do this
SharedPreferences sharedPreferences = getSharedPreferences("your_name_of_shared_preferences", 0);
int result = sharedPreferences.getInt("result", defaultValue);
resultValue is your quiz result and defaultValue is needed in situation when you didn't write anything to sharedPreferences under that key.
Upvotes: 0