Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate score for a quiz on android
    primarykey
    data
    text
    <p>I am doing a <code>quiz</code>, in that I have 3 activities <code>Question1</code>, <code>Question2</code>, <code>Question3</code>,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.</p> <p>I have done some research and <code>SHAREDPREFERENECES</code> seem to be the solution. But i dont know how to use it can anyone help please. <code>Question1.java</code> code is below: Assume that <code>btnAnswer1a</code> is the correct answer, how do i save the data and then display it in a <code>textview</code> in the last activity.</p> <pre><code> 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload