0

I'm a beginner and i'm trying to make a quiz game without database and multiple activities. I want to get the scoring system as a textview in all the activities and it should change according to right or wrong answer. I've been trying to figure it out for over a week now and I only think I wasted that time. Here is a sample code. All help will be appreciated.

my Page4.java

    public class Page4 extends AppCompatActivity {
int score = 0;
TextView scored1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
    setContentView(R.layout.activity_page4);
    scored1 = (TextView) findViewById(R.id.score);
    scored1.setText(String.valueOf(score));
}
public void nocircle(View view){
    score = score + 5;
    scored1.setText(String.valueOf(score));
    Intent intent = new Intent(this,Page5.class);
    intent.putExtra("score",String.valueOf(score));
    startActivity(intent);
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
            0, view.getWidth(), view.getHeight());
    startActivity(intent, options.toBundle());
}
}

activity_page4

    <LinearLayout
        android:layout_width="50dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:background="#05083e"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Score"
            android:id="@+id/tvLabelScore"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="#f6f6f6"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/score"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textColor="#e5e5e5"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

    <TextView
    android:layout_width="180dp"
    android:layout_height="100dp"
    android:layout_below="@+id/imageView10"
    android:text="NO"
    android:gravity="center"
    android:fontFamily="casual"
    android:textSize="70sp"
    android:textColor="#fff"
    android:layout_toRightOf="@+id/answeryes"
    android:onClick="nocircle"
    />
</RelativeLayout>

When the new activity Page5.class begins, the textview is 5...but when I click the button the score gets reset and only 5 points are added. I want it in such a way that the points add the Page4.java score and this Page5.java score and display in the textview....and continue with the same for the next activity.

Page5.java code

    public class Page5 extends AppCompatActivity {


int score = 5;
TextView scored;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);;
    setContentView(R.layout.activity_page5);
    scored = (TextView) findViewById(R.id.score1);

    Intent mintent = getIntent();
    String score = mintent.getStringExtra("score");
    scored.setText(String.valueOf(score));

}
public void rightanswer(View view){


    scored.setText(String.valueOf(score));
    Intent intent = new Intent(this, Page6.class);
    intent.putExtra("ta_score",String.valueOf(score));
    startActivity(intent);
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
            0, view.getWidth(), view.getHeight());
    startActivity(intent, options.toBundle());

}


 }

and finally my activity_page5

      <LinearLayout
    android:layout_width="50dp"
    android:layout_height="80dp"
    android:layout_weight="1"
    android:background="#05083e"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:id="@+id/tvLabelScore"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#f6f6f6"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/score1"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#e5e5e5"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"

    android:onClick="rightanswer"
    />

Any help would be awesomely appreciated with kind words of praise.

1 Answers1

0

It looks like you aren't actually doing the addition of the previous score (page 4) to the current score (page 5) anywhere.

Also, here is a link to several ways you can share data between activities, although you're already using one of them.

Update:

To add them you just to the addition with the values you already have, so:

public void rightanswer(View view){

    score += Integer.parseInt(scored.getText()); //add them right there

    scored.setText(String.valueOf(score));
    Intent intent = new Intent(this, Page6.class);
    intent.putExtra("ta_score",String.valueOf(score));
    startActivity(intent);
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
        0, view.getWidth(), view.getHeight());
    startActivity(intent, options.toBundle());

}

Community
  • 1
  • 1
Dave
  • 321
  • 1
  • 8
  • The addition code is what I don't know...can you help me with that? – LateBeginner May 15 '16 at 19:44
  • Also, you can pass values in your intent as an integer. I would suggest you do that for your scores and then use intent.getIntExtra() – Dave May 15 '16 at 19:53