0

This is my first project in Android studio. code is a little messy. Im creating a score keeping app for a card game I play with family members.

Im having a problem adding score to main activity from bid activity. how would you store the textview to continue to add to?

Score_activity java code:

          public class score_activity extends AppCompatActivity {
          // creating an object of the text view
          TextView scoreA;
          TextView scoreB;
         TextView tvA;
          TextView tvB;

        @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_score);
       // assigning the outputs of the user to the object
       scoreA = (TextView) findViewById(R.id.scoreA);
       scoreB = (TextView) findViewById(R.id.scoreB);
       tvA = (TextView) findViewById(R.id.nameA);
       tvB = (TextView) findViewById(R.id.nameB);

        Bundle bundle = getIntent().getExtras();
        String text1 = bundle.getString("Team A");
        String text2 = bundle.getString("Team B");
        String text3 = getIntent().getStringExtra("ScoreA");
         String text4 = getIntent().getStringExtra("ScoreB");
         // setting the fetched data to the corresponding text views

       tvA.setText(text1);
       tvB.setText(text2);
       scoreA.setText(text3);
        scoreB.setText(text4);
       // this method is the logic that increases the value in the text 
       view by one on every click for team A
       }

Bid Activity code

       public void scoreTotal(View view) {

        String nameA = tvTeamA.getText().toString();
        String nameB = tvTeamB.getText().toString();
        int bida = Integer.parseInt(bidA.getText().toString());
        int tricksTotalA = 
      Integer.parseInt(tricksA.getText().toString());

        int totalB = 25 - tricksTotalA;
        int totalScoreBA = mentionteamA + tricksTotalA;
        int totalScoreBB = mentionteamB + totalB;



        if (bida > totalScoreBA) {
            scoreA2 = (bida * -1);

            String scoreBoardA = String.valueOf(scoreA2);
            String scoreBoardB = String.valueOf(totalScoreBB);

            Intent i = new Intent(bid_activity.this, 
        score_activity.class);
            i.putExtra("Team A", nameA);
            i.putExtra("Team B", nameB);
            i.putExtra("ScoreA", scoreBoardA);
            i.putExtra("ScoreB", scoreBoardB);
            // starting the activity
            startActivity(i);
        }

        if (bida <= totalScoreBA) {
            scoreA2 = totalScoreBA;
            String scoreBoardA = String.valueOf(scoreA2);
            String scoreBoardB = String.valueOf(totalScoreBB);
            Intent i = new Intent(bid_activity.this, 
     score_activity.class);
            i.putExtra("Team A", nameA);
            i.putExtra("Team B", nameB);
            i.putExtra("ScoreA", scoreBoardA);
            i.putExtra("ScoreB", scoreBoardB);
            // starting the activity
            startActivity(i);
        }
        if (tricksTotalA == 25) {
            scoreA2 = totalScoreBA;
            int displayB = 0;

            String scoreBoardA = String.valueOf(scoreA2);
            String scoreBoardB = String.valueOf(displayB);
            Intent i = new Intent(bid_activity.this, 
      score_activity.class);
            i.putExtra("Team A", nameA);
            i.putExtra("Team B", nameB);
            i.putExtra("ScoreA", scoreBoardA);
            i.putExtra("ScoreB", scoreBoardB);
            // starting the activity
            startActivity(i);

        }

2 Answers2

0

One idea is to call

startActivityForResult(i)

instead of

startActivity(i)

Then you can pass a result in BidActivity and capture it in ScoreActiivty's onActivityResult call.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
0

I am just showing how to pass data from your bid activity to score activity.

bid activity

Intent i = new Intent(bid_activity.this, score_activity.class);
i.putExtra("ScoreA2", scoreA2);
i.putExtra("ScoreB2", scoreB2);
startActivity(intent);

score activity

int scoreA2 = getIntent().getIntExtra("ScoreA2");
int scoreB2 = getIntent().getIntExtra("scoreB2");

And for double click, you can follow this thread.

Eishon
  • 1,274
  • 1
  • 9
  • 17
  • I'm trying to take the ScoreA2 and ScoreB2 at bottom of bid activity and pass it to score activity and add it to the scoreA & scoreB textview. Have to bundle it and then pass it over just unsure on how to get bundle and set textview – Itz_Orange Jul 09 '21 at 03:04
  • In your case bundle is unnecessary in my opinion. Passing data using Intent will be easier and straightforward for your purpose. – Eishon Jul 09 '21 at 04:37
  • updated my code, when i press the scoretotal button from bidactivity to pass over the added score to set to textview in scoreactivity. I pass over via intent has a string or int? where do i put this getintent to set values to textview in the oncreate? need this textvia to store value until reset by rset button back to 00 – Itz_Orange Jul 10 '21 at 06:09
  • You can send the value as int or String, just make sure textview gets String value. You have to put this code in onCreate method before assigning the value to the textview so that the passed value is ready for showing in the view. – Eishon Jul 10 '21 at 13:09
  • Assigning it to oncreate cause project to crash as this is my first page and it bounces from this to two bidactivities. – Itz_Orange Jul 10 '21 at 13:35
  • Edit code: I got the code in place to transfer score back to score activity using if statements, Im now looking at storing the main score on score activity till reaches 150, would you use a `if(score < 150) Intent i = new Intent(bid_activity.this, score_activity.class); i.putExtra("ScoreA2", scoreA2); i.putExtra("ScoreB2", scoreB2); startActivity(intent);` to pass over to bid activity to add to? – Itz_Orange Jul 11 '21 at 06:45