Frage

I have sought for this for a long time with no chance, I will now have the permission to ask this question.

I am trying to developing a quiz in Android whereas I have a problem when updating the score. At the moment, I have two classes; MainActivity and Question002 (and in MainActivity there is the first question).

In my MainActivity I have an int called currentPoint. I have created four buttons and by clicking on the right button, there is an increment (currentPoint++). Then in class Question002, I have the following code:

    MainActivity a = new MainActivity();

Upon answering correct I've said:

    a.currentPoint++;

However, this returns its' first value meaning the initialization (which is by standard 0) instead of 1, if the first question is correctly answered. How do I pass the value 1 to my Question002 class, when the first question is answered correctly and it shows the next activity (thus next class)?

War es hilfreich?

Lösung

First Create an Application Class

class MyApplication extends Application{
 public int currentScore;
 private SharedPreferences sp;

    @Override
public void onCreate()
{
    super.onCreate();
    sp = getSharedPreferences(getResources().getString(R.string.app_name),
            MODE_PRIVATE);
        currentScore = sp.getInt("score",0);

}

    public void updateScore(int increment)
    {
       currentScore = currentScore+increment;
       sp.edit().putInt("score", currentScore).commit();
    }
 }

Now On MainActivity add the following code -

 class MainActivity extends Activity{
 public MyApplication application;
     @Override
    protected void onCreate(Bundle arg0)
    {
       application = (MyApplication) getApplication();

        // to get current score 
       int currentScore = application.currentScore;
       // to update currentScore
        application.updateScore(1);
    }
 }

Same On Now On Question002 add the following code -

 class Question002 extends Activity{
 public MyApplication application;
     @Override
    protected void onCreate(Bundle arg0)
    {
       application = (MyApplication) getApplication();

        // to get current score 
       int currentScore = application.currentScore;
       // to update currentScore
        application.updateScore(1);
    }
 }

now finaly in the add android:name property to the application tag -

  <application
    android:name="yourpackagename.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

Your score is stored in MyApplication class. All the Activity can access it by via getApplication() method after onCreate() has called. No need to pass score via intent. Score also saved in sharedprefernce and loaded from it too.

Andere Tipps

you can use sharedpreference to maintain score but if you want to use main activity , then declare currentpoint as public static variable and then increment it like

MainActivity.currentPoint++;

if(your answer correct)
{
currentPoint = currentPoint+1;

Intent myIntent = new Intent(MainActivity.this, Question002.class);
myIntent.putExtra("result", currentPoint );
startActivity(myIntent);


}

In class Question002

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("result", 0);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top