In: Computer Science
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);
}
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