Question

In: Computer Science

How to get the vote count to display in another activity? This is for android studio...

How to get the vote count to display in another activity? This is for android studio with Java codes.

private int yesVoteCount = 0;
    private int noVoteCount = 0;
    private int resetVotes = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Use res ID to retrieve inflated objects and assign to variables
        mYesButton = findViewById(R.id.yes_button);
        mNoButton = findViewById(R.id.no_button);
        mResetButton = findViewById(R.id.reset_button);
        mSurveyQuestion = findViewById(R.id.survey_question);
        mYesCount = findViewById(R.id.yes_count);
        mNoCount = findViewById(R.id.no_count);

        if (savedInstanceState != null) { // Saving data
            yesVoteCount = savedInstanceState.getInt(YES_INDEX, yesVoteCount);
           noVoteCount = savedInstanceState.getInt(NO_INDEX, noVoteCount);

            mYesCount.setText(String.valueOf(yesVoteCount));
            mNoCount.setText(String.valueOf(noVoteCount));
        }
        // Listener method for when button is clicked
        mYesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addVote();
            }
        });

        mNoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addVote();
            }
        });
        // Resetting vote count
        mResetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mYesCount.setText(String.valueOf(resetVotes)); //Resetting votes to zero
                mNoCount.setText(String.valueOf(resetVotes));
                yesVoteCount = 0; // Resetting variable back to zero
                noVoteCount = 0;
            }
        });
    }
    // Add vote when button is pressed
    private void addVote() {
        if (mYesButton.isPressed()) {
            yesVoteCount++;
            mYesCount.setText(String.valueOf(yesVoteCount)); // Adding votes
        } else if (mNoButton.isPressed()) {
            noVoteCount++;
            mNoCount.setText(String.valueOf(noVoteCount));
        }
    }
    // Data restore upon rotation
        @Override
        protected void onSaveInstanceState(Bundle outBundle) {
            super.onSaveInstanceState(outBundle);
            outBundle.putInt(YES_INDEX, yesVoteCount);
            outBundle.putInt(NO_INDEX, noVoteCount);
        }

Solutions

Expert Solution


  
   private int yesVoteCount = 0;
private int noVoteCount = 0;
private int resetVotes = 0;
   private Button voteActivityBtn; // let voteActivityBtn be a button (defined in layout file also) which invokes the activity to display the vote count
  
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Use res ID to retrieve inflated objects and assign to variables
mYesButton = findViewById(R.id.yes_button);
mNoButton = findViewById(R.id.no_button);
mResetButton = findViewById(R.id.reset_button);
mSurveyQuestion = findViewById(R.id.survey_question);
mYesCount = findViewById(R.id.yes_count);
mNoCount = findViewById(R.id.no_count);
      
       voteActivityBtn = findViewById(R.id.voteActivityBtn); // suppose voteActivityBtn is defined in layout file as id "voteActivityBtn"

if (savedInstanceState != null) { // Saving data
yesVoteCount = savedInstanceState.getInt(YES_INDEX, yesVoteCount);
noVoteCount = savedInstanceState.getInt(NO_INDEX, noVoteCount);

mYesCount.setText(String.valueOf(yesVoteCount));
mNoCount.setText(String.valueOf(noVoteCount));
}
// Listener method for when button is clicked
mYesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addVote();
}
});

mNoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addVote();
}
});
// Resetting vote count
mResetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mYesCount.setText(String.valueOf(resetVotes)); //Resetting votes to zero
mNoCount.setText(String.valueOf(resetVotes));
yesVoteCount = 0; // Resetting variable back to zero
noVoteCount = 0;
}
});


// calling VoteActivity
       voteActivityBtn.setOnClickListener(new View.OnClickListener() {
           @Override
public void onClick(View view) {
// Suppose VoteActivity is the name of the Activity class to display the vote count
               Intent voteActivity = new Intent(); // create a new Intent object
               voteActivity.setClass(this, VoteActivity.class); // set the class to VoteActivity, the activity to call
               // set the parameters to send to the VoteActivity
               voteActivity.putExtra("yesVote", yesVoteCount);
               voteActivity.putExtra("noVote",noVoteCount);
              
               startActivity(voteActivity); // call the voteActivity , this will invoke the VoteActivity
              
}
       });

}
// Add vote when button is pressed
private void addVote() {
if (mYesButton.isPressed()) {
yesVoteCount++;
mYesCount.setText(String.valueOf(yesVoteCount)); // Adding votes
} else if (mNoButton.isPressed()) {
noVoteCount++;
mNoCount.setText(String.valueOf(noVoteCount));
}
}
// Data restore upon rotation
@Override
protected void onSaveInstanceState(Bundle outBundle) {
super.onSaveInstanceState(outBundle);
outBundle.putInt(YES_INDEX, yesVoteCount);
outBundle.putInt(NO_INDEX, noVoteCount);
}
}//end of Activity


In VoteActivity create 2 int data yesVoteCount and noVoteCount
In VoteActivity's onCreate method have the below statements to get the yesVoteCount and noVoteCount passed by the above activity

Intent caller = getIntent();
yesVoteCount = caller.getIntExtra("yesVote", 0); // this will set yesVoteCount to the value passed as yesVote parameter, if found else it will be set to default value of 0
noVoteCount = caller.getIntExtra("noVote",0); // this will set noVoteCount to the value passed as noVote parameter, if found else it will be set to default value of 0
The above two variables can be used to display the vote counts in the VoteActivity


Related Solutions

Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
the code base you will be working with involves an Android application. (Android Studio) application name...
the code base you will be working with involves an Android application. (Android Studio) application name " SharingApp" In the application’s current state: A user of the app is able to create and edit a profile with a unique username and an email address. A user of the app is able to login and logout. An owner is able to record the items they own and wish to share. A bidder is able to place bids on items they wish...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used by the customers to book cinema tickets. Only users of 15 years old and above are allowed to book cinema tickets for a particular film. The user can book more than one ticket where ticket prices vary between 20 and 50 AED. Your app contains tree Activities. The first activity is a launching activity containing a logo and a button start. When the user...
android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT