Scoring of trivia game becomes inaccurate after 2nd game - java

I have a trivia game that is 10 timed questions. A wrong answer is supposed to output 0 points. A correct answer is supposed to output the remaining of the 20,000 milliseconds converted to a 100 point scale.
Right now the game works on the first game. Then, if the user keeps playing for a second time, the points for each question becomes incorrect. For example, incorrect answers begin to have a variety of points from 1 to 100 and also correct answers sometimes have 0 points and also sometimes wrong point totals. So I am guessing some variable is possibly not resetting on the second game so it is causing the points to be a little off.
I posted all the code that deals with the algorithm. My question is how I can get this code to output the correct answer no matter how many times the user plays before closing the app.
QuestionView.java
public class QuestionView extends Activity {
int correctAnswers = 0;
int wrongAnswers = 0;
int answer = 0;
int i = 0;
long score = 0;
long startTime = 20000;
long interval = 1000;
long points;
boolean timerHasStarted = false;
String category;
Button answer1, answer2, answer3, answer4;
TextView question, pointCounter, questionNumber, timeCounter, timeremaining;
AdView adView;
ArrayList<Question> queries;
public static ArrayList<Long> pointsPerQuestion = new ArrayList<Long>(10);
Timer cdTimer;
ProgressBar bar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.questionview);
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
answer3 = (Button)findViewById(R.id.answer3);
answer4 = (Button)findViewById(R.id.answer4);
question = (TextView)findViewById(R.id.question);
category = getIntent().getStringExtra("category");
queries = getIntent().getParcelableArrayListExtra("queries");
questionNumber = (TextView)findViewById(R.id.questionnumber);
timeremaining = (TextView)findViewById(R.id.timeremaining);
cdTimer = new Timer(startTime, interval);
bar = (ProgressBar)findViewById(R.id.progressbar);
bar.setIndeterminate(false);
bar.setMax(20000);
tracker.sendView("/QuestionView - Started Quiz");
loadQuestion();
}
public void loadQuestion() {
if(i == 10) {
cdTimer.cancel();
endQuiz();
} else {
if(!timerHasStarted) {
cdTimer.start();
timerHasStarted = true;
} else {
cdTimer.start();
timerHasStarted = false;
}
answer = queries.get(i).getCorrectAnswer();
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(0);
if(answer == 0) {
correctAnswers++;
correct();
} else {
wrongAnswers++;
incorrect();
}
}
});
answer2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(1);
if(answer == 1) {
correctAnswers++;
correct();
} else {
wrongAnswers++;
incorrect();
}
}
});
answer3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(2);
if(answer == 2) {
correctAnswers++;
correct();
} else {
wrongAnswers++;
incorrect();
}
}
});
answer4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(3);
if(answer == 3) {
correctAnswers++;
correct();
} else {
wrongAnswers++;
incorrect();
}
}
});
}
}
public ArrayList<Question> getQueries() {
return queries;
}
public static ArrayList<Long> getPointsPerQuestion() {
return pointsPerQuestion;
}
public void correct() {
pointsPerQuestion.add(points);
score = score + points;
i++;
loadQuestion();
}
public void incorrect() {
long zero = 0;
pointsPerQuestion.add(zero);
i++;
loadQuestion();
}
public class Timer extends CountDownTimer {
public Timer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
points = 0;
if(i >= 9) {
cdTimer.cancel();
pointsPerQuestion.add(points);
endQuiz();
} else {
wrongAnswers++;
incorrect();
}
}
#Override
public void onTick(long millisUntilFinished) {
bar.setProgress((int) millisUntilFinished);
points = millisUntilFinished / 200;
timeremaining.setText("Score remaining: " + points);
if(i < 10) {
questionNumber.setText("Question " + (i + 1) + " of 10");
}
}
}
public void endQuiz() {
Intent intent = new Intent(QuestionView.this, Results.class);
intent.putExtra("correctAnswers", correctAnswers);
intent.putExtra("wrongAnswers", wrongAnswers);
intent.putExtra("score", score);
intent.putExtra("pointsPerQuestion", pointsPerQuestion);
intent.putParcelableArrayListExtra("queries", queries);
intent.putExtra("category", category);
startActivity(intent);
}
}
Here is a Question object:
exodus.add(new Question("6th", "3rd", "5th", "4th", 2, "Name that plague: All livestock dies", -1, "Exodus 9:6"));
In my Results class, which is where the points per question is displayed, this is the line that displays the points:
scoreTV.setText("You received " + QuestionView.getPointsPerQuestion().get(x) + " out of a possible 100 points.");
Everything else is pretty much in my QuestionView above. Let me know if you still need something else.
First 3 Strings are answers A (index 0), B (index 1), C (index 2) and D (index 3). The first int is the correct answer. The 5th String is the question. The second int is set to the answer selected by user during quiz. Last String is the bible verse where the answer is found.

Related

Android Broadcast Receiver as inner static class Passing a String

Probably you want to jump to Update 2 and check the code if needed
I am building a barcode scanner and having difficulty in passing data that I have captured from an inner class that extends BroadcastReceiver to MainActivity class, I do understand the difference between static and non static objects, but I got stuck.
Cant invoke my logic method from the inner class.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState){...}
public void Logic(String result){// Do something...}
//Inner Class
public static class ScanResultReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {...
// data here captured fine!
// Here I want to send my data to MainActivity Logic(result)
Logic(result);
}
}
If I make "Logic()" as Static method, I get a lot of errors regards to calling non static from static method from Toaster/variables..etc
Update
This method is inside MainActivity, I do want to call it from the inner class
public void Logic(String result) throws Exception {
//prepare the results
if (mDecodeResult.decodeValue.substring(0, 1).equals("{") && mDecodeResult.decodeValue.substring(mDecodeResult.decodeValue.length() - 1).equals("}")) {
if (!(mDecodeResult.decodeValue.equals("SCAN AGAIN"))) {
mDecodeResult.decodeValue = mDecodeResult.decodeValue.substring(1);
mDecodeResult.decodeValue = mDecodeResult.decodeValue.substring(0, mDecodeResult.decodeValue.length() - 1);
}
}
if (mDecodeResult.decodeValue.equals("SCAN AGAIN")) {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received! Please Scan Again", Toast.LENGTH_SHORT);
toast.show();
} else if (mDecodeResult.decodeValue != null && tourFlag) {
String formattedDate = getTime();
String scanContent = mDecodeResult.decodeValue;
boolean found = false;
if (ForcedOrRandom.equals("Random")) {
String[] b;
for (String l : ToBeScanned) {
b = l.split(":");
if (scanContent.equals(b[0])) {
Log.d("remove", "scanned: " + scanContent);
Log.d("remove", "remove : " + b[0]);
found = true;
}
}
} else if (ForcedOrRandom.equals("Forced")) {
String[] b;
for (String I : FTobeScannedNext) {
b = I.split(":");
if (scanContent.equals(b[0])) {
Log.d("remove", "scanned: " + scanContent);
Log.d("remove", "remove : " + b[0]);
found = true;
}
}
}// end Skip/Forced
if (listLoaded && found) {
theResult[resultCount].setTourID(currentTourId);
theResult[resultCount].setBarcode(scanContent);
BarcodeObject a = getBarcodeInfo(scanContent);
if (ForcedOrRandom.equals("Random")) {
} else {
if (myTimer != null) {
myTimer.cancel();
Timer = (TextView) findViewById(R.id.timertext);
Timer.setText("");
PlayOrPause.setVisibility(View.INVISIBLE);
}
boolean isTimed = a.getForceNextBarCode().equals("");
if (!(isTimed)) {
PlayOrPause = (ImageButton) findViewById(R.id.PlayPause);
PlayOrPause.setVisibility(View.VISIBLE);
PlayOrPause.setImageResource(R.drawable.pause);
final AlertDialog.Builder timealert = new AlertDialog.Builder(this);
PlayOrPause.setEnabled(true);
long duration = Integer.parseInt(a.getForceNextBarCode());
duration = duration * 60000;
myTimer = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisuntilFinished) {
int seconds = (int) (millisuntilFinished / 1000) % 60;
int minutes = (int) ((millisuntilFinished / (1000 * 60)) % 60);
Timer = (TextView) findViewById(R.id.timertext);
Timer.setText(minutes + ":" + seconds);
timeLeft = millisuntilFinished;
}
String value = "";
#Override
public void onFinish() {
Timer = (TextView) findViewById(R.id.timertext);
theResult[resultCount].setScanstatus(scanStatusTimeElapsed);
timealert.setTitle("Site Secure");
timealert.setMessage("Time Elapsed! Enter reason");
// Set an EditText view to get user input
final EditText input = new EditText(MainActivity.this);
timealert.setView(input);
timealert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
// Do something with value!
while (value.equals("")) {
timealert.setView(input);
timealert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
}
});
}
theResult[resultCount].setComments(value);
}
});
timealert.setIcon(android.R.drawable.ic_dialog_alert);
timealert.show();
Timer.setText(R.string.Time_Elapsed);
}
};
myTimer.start();
}
}
theResult[resultCount].setBarcodeID(a.getBarCodeId());
theResult[resultCount].setDateScanned(formattedDate);
theResult[resultCount].setSkipped(getResources().getString(R.string.Scanned));
}// end big if listLoaded && found
contentTxt.setText(scanContent);
Toaster(getResources().getString(R.string.TScan_Complete));
if (mainScanCounter == 0) {
if (tourDecider(scanContent)) {//tour decider is called to determine if this is boolJanamScanner random or forced tour
tourId = scanContent;
if (!(readFileOffline(siteSecurePath + "/doneTourNumber.txt").equals(""))) {
SYNC.setEnabled(true);
}
}
} else if (mainScanCounter > 0) {
if (ForcedOrRandom.equals("Random")) {
ListManager(scanContent);
} else {
ForcedListManager(scanContent);
}
}
} else if (mDecodeResult.decodeValue != null && officerScanFlag) {
TextView officertextview = (TextView) findViewById(R.id.officerid);
UserObject theofficer = getUserInfo(mDecodeResult.decodeValue);
if (theofficer == null) {
popUps("Error", "Invalid Officer ID, Please Rescan", "TITLE");
officerScan.setEnabled(true);
} else if (theofficer != null) {
// officer ID found need to store it for backup
officerId = theofficer.getOfficerid();
makeFileOffline(officerId, "officerID");
officertextview.setText(theofficer.getUsername());
officerScanFlag = false;
startTimersOfficerID = getTime();
tourBtn.setEnabled(true);
}
}
if (mDecodeResult.decodeValue != null && exceptionFlag) {
Log.d("check", "exception was clicked");
String ex_result = mDecodeResult.decodeValue;
for (int i = 0; i < theExceptions.length; i++) {
if (!(theExceptions[i].getBarcode().equals(ex_result))) {
String refnum = theExceptions[i].getRefNum();
i = theExceptions.length;
theResult[resultCount - 1].setException(refnum);
}
}
exceptionFlag = false;
Toaster(getResources().getString(R.string.TScan_Complete));
}
} // Logic Ends
Update 2
Not sure if I need to have another thread for this but I will put what I have found, my issue have narrowed to the following:
I am waiting on an intent called
<action android:name="device.scanner.USERMSG" />
with a permission
android:permission="com.permission.SCANNER_RESULT_RECEIVER"
now my issue
if a user tap button and released in less than .5 second onKeyup() event will be fired before my onReceive() that is inside the static class which is extends BroadcastReceiver, and that causes problem because Logic() will be invoked before updating the String inside onReceive()
if user hold the button long enough, onReceive will be invoked and everything is good and happy.
How can I make sure that onReceive() always invoked first?
public boolean onKeyUp(int keycode, KeyEvent event) {
if (keycode == 221 || keycode == 220 || keycode == 222) {
Logic(result);
}
return true;
}
Move this line of code:
public void Logic(String result){// Do something...}
inside your class ScanResultReceiver and it will work for sure. Your code should look like this:
public static class ScanResultReceiver extends BroadcastReceiver {
public ScanResultReceiver() {
//empty constructor
}
#Override
public void onReceive(Context context, Intent intent) {...
// data here captured fine!
// Here I want to send my data to MainActivity Logic(result)
Logic(result);
}
public void Logic(String result){/* ... */}
}

App crashes when I try to set the TextView

I have this class I've named Scoreboard that looks like this:
public class Scoreboard {
private int mScore = 0;
private int mHighScore = 0;
public void addPoint() {
mScore += 1;
}
public int getHighScore() {
return mHighScore;
}
public void setHighScore(int highScore) {
mHighScore = highScore;
}
public int getScore() {
return mScore;
}
public void setScore(int score) {
mScore = score;
}
}
And I have a piece of code in my acticity that manipulates this class everytime the user passes a ball through a rectangle's gap and changes the TextView accordingly to show the user the score:
//Keeps track of score
if (rectWhite1.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite1.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run3333!!!");
}
if (rectWhite2.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite2.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run2222!!!");
}
if (rectWhite3.getTheRight() > Math.round(mBallPos.x) - 5 && rectWhite3.getTheRight() < Math.round(mBallPos.x) + 5) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore());
}
});
Log.d(TAG, "If statement has been run1111!!!");
}
Everytime I run the app it crashes whenever I try to use any method pertaining to the Scoreboard class I made. When I try to add a point it crashes, when I try to get the current score it crashes, etc. So my question is how do I avoid this from happening and change the score dynamically?

CountDownTimer Android issue

I'm trying to implement a CountDownTimer in an Android Application. This timer will, while running, countdown from a value, than reset, than countdown from a different value. Switching back and force between values until either a set number of rounds have elapsed or the stop button has been pressed. I can get the CountDownTimer samples to work, but I guess I'm missing something here. Below is the applicable button press code;
CounterState state = CounterState.WORKOUT;
private WorkoutTimer workoutTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_stopwatch);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set up OnClickListeners
((Button) findViewById(R.id.start_button)).setOnClickListener(this);
((Button) findViewById(R.id.stop_button)).setOnClickListener(this);
((Button) findViewById(R.id.reset_button)).setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.start_button:
if (!timer_running) {
timer_running = true;
Log.d(TAG, "clicked on Start Button");
// If the state is unknown, set it to Workout first
int State = state.getStateValue();
if (State == 0) {
state.setStateValue(1);
}
workoutTimer.start();
}
break;
case R.id.stop_button:
Log.d(TAG, "clicked on Stop Button");
if (timer_running); {
timer_running = false;
workoutTimer.cancel();
}
break;
private class WorkoutTimer extends CountDownTimer{
public WorkoutTimer(long interval) {
super(getThisTime(), interval);
Log.d(TAG, "WorkoutTimer Constructed...");
}
TextView digital_display = (TextView) findViewById(R.id.digital_display);
TextView numOfRounds = (TextView) findViewById(R.id.number_of_rounds);
public void onFinish() {
int State = state.getStateValue();
int roundsLeft = 0;
if (State == 1) {
state.setStateValue(2);
} else {
state.setStateValue(1);
}
decrementRounds();
try {
roundsLeft = Integer.parseInt(numOfRounds.getText().toString());
} catch(NumberFormatException nfe) {
roundsLeft = 999;
}
if (roundsLeft > 0 || roundsLeft != 999) {
workoutTimer.start();
}
}
public void onTick(long millisUntilFinished) {
final long minutes_left = ((millisUntilFinished / 1000) / 60);
final long seconds_left = (millisUntilFinished / 1000) - (minutes_left * 60);
final long millis_left = millisUntilFinished % 100;
String time_left = String.format("%02d:%02d.d", minutes_left, seconds_left,
millis_left);
digital_display.setText(time_left);
}
}
private long getThisTime() {
long time = 0;
TextView workout_time = (TextView) findViewById(R.id.workout_time);
TextView rest_time = (TextView) findViewById(R.id.rest_time);
switch(state) {
case WORKOUT:
try {
time = Integer.parseInt(workout_time.getText().toString());
} catch(NumberFormatException nfe) {
time = 999;
}
// time = 90;
Log.d(TAG, "Workout time = " + time);
break;
case REST:
try {
time = Integer.parseInt(rest_time.getText().toString());
} catch(NumberFormatException nfe) {
time = 999;
}
// time = 30;
Log.d(TAG, "Rest time = " + time);
break;
case UNKNOWN:
time = 0;
break;
}
return time;
}
Everything starts up okay, but crashes when I click either button. If I comment out my calls to the workoutTimer, no crash. I never see my log in the constructor of the workoutTimer class, so obviously I'm missing something here. Any help would be appreciated.
-Ian
You have not initialized your workoutTimer. You need to add the following line in your onCreate method.
workoutTimer = new WorkoutTimer(...);

Android CountDown Timer won't stop [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Ok so here is my problem. I am working on a trivia game that uses a question counter to display 10 questions and once that happens it calls the finalscore class. I have implemented a timer that resets for every question and if time runs up it proceeds to the next question. However if nothing is clicked on the final question it will keep looping back to the finalscore class even after starting a new game.
public class game extends Activity implements OnClickListener {
// int[] anArray;
int[] anArray2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int numberOfQuestionsDisplayedCounter = 0;
int nextOrFinishValue = 0;
int randomValueZeroToThree = 0;
int randomValue0to29 = 0;
boolean alreadyDisplayQuestion = false;
boolean validQuestionNumber = false;
boolean rightButton1 = false;
boolean rightButton2 = false;
boolean rightButton3 = false;
boolean rightButton4 = false;
String questionFromDatabase;
String answer1FromDatabase;
String answer2FromDatabase;
String answer3FromDatabase;
String answer4FromDatabase;
String displayedQuestionFromDatabase = "question ";
String displayedAnswer1FromDatabase = "answer1a ";
String displayedAnswer2FromDatabase = "answer2a ";
String displayedAnswer3FromDatabase = "answer3a ";
String displayedAnswer4FromDatabase = "answer4a ";
int categoryId = 0;
int questionId = 0;
int currentScore = 0;
private CountDownTimer mCountDown;
public void onCreate(Bundle savedInstanceState) {
int randomInt = 1;
super.onCreate(savedInstanceState);
categoryId = com.triviagame.Trivia_Game.categoryIdInMainMenu;
// System.out.println("categoryID =" + categoryId);
setContentView(R.layout.displayquestion);
// gathers question and answers from database
gatherInfoBeforePopulating(); // includes generating random number
// between getting info from database 1
// to 30
// displays question, answers, score
populateWithQuestionInfo(displayedQuestionFromDatabase,
displayedAnswer1FromDatabase, displayedAnswer2FromDatabase,
displayedAnswer3FromDatabase, displayedAnswer4FromDatabase,
nextOrFinishValue);
// Sets buttons and listers for the buttons with the 4 possible answers
View answer1button = findViewById(R.id.answer1button);
answer1button.setOnClickListener(this);
View answer2button = findViewById(R.id.answer2button);
answer2button.setOnClickListener(this);
View answer3button = findViewById(R.id.answer3button);
answer3button.setOnClickListener(this);
View answer4button = findViewById(R.id.answer4button);
answer4button.setOnClickListener(this);
final TextView myCounter = (TextView) findViewById(R.id.tv);
mCountDown = new CountDownTimer(21000, 1000) {
#Override
public void onFinish() {
questionCheck();
resetTimer();
}
#Override
public void onTick(long millisUntilFinished) {
myCounter.setText("Time left: "
+ String.valueOf(millisUntilFinished / 1000));
}
}.start();
}
// Displays question, answers, score
public void populateWithQuestionInfo(String question, String answer1,
String answer2, String answer3, String answer4,
int nextQuestionOrFinish) {
// populate the question label
TextView questionLabel = (TextView) findViewById(R.id.question);
questionLabel.setText(question);
// populate answer1
Button buttonAnswer1 = (Button) findViewById(R.id.answer1button);
buttonAnswer1.setText(answer1);
// populate answer2
Button buttonAnswer2 = (Button) findViewById(R.id.answer2button);
buttonAnswer2.setText(answer2);
// populate answer3
Button buttonAnswer3 = (Button) findViewById(R.id.answer3button);
buttonAnswer3.setText(answer3);
// populate answer4
Button buttonAnswer4 = (Button) findViewById(R.id.answer4button);
buttonAnswer4.setText(answer4);
numberOfQuestionsDisplayedCounter++;
}
// Generates a random number between 0 and 3 to be used to display the
// possible answers randomly
int RandomGeneratorZeroToThree() {
int randomInt = 0;
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
randomInt = randomGenerator.nextInt(4);
}
return randomInt;
}
// Generates a random number between 0 and 29 to be used to select the
// question to be displayed
int RandomGenerator0To29() {
int randomInt2 = 0;
Random randomGenerator2 = new Random();
for (int idx2 = 1; idx2 <= 10; ++idx2) {
randomInt2 = randomGenerator2.nextInt(30);
}
return randomInt2;
}
// returns true if the question has already been displayed
// returns false if the question has not been already displayed
boolean questionsAlreadyDisplayed(int randomValue0to29) {
for (int i = 0; i < 10; i++) {
if (anArray2[i] == randomValue0to29) {
return true; // question already displayed
}
}
anArray2[numberOfQuestionsDisplayedCounter] = randomValue0to29; // questionId
// added
// to
// array
// of
// displayed
// questions
return false; // random number can be used as it has been used already
}
// Connects to the database to gather the question and 4 possible answers.
// Uses DataBaseHelper class
void getInfoFromDatabase(int categoryId, int questionId) {
DataBaseHelper myDbHelper = new DataBaseHelper(
this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
questionFromDatabase = myDbHelper.getQuestion(categoryId,
questionId);
displayedQuestionFromDatabase = questionFromDatabase;
System.out
.println("questionFromDatabase = " + questionFromDatabase);
answer1FromDatabase = myDbHelper.getCorrectAnswer(categoryId,
questionId); // correct answer from database
System.out.println("correct answer from db = "
+ answer1FromDatabase);
answer2FromDatabase = myDbHelper.getWrongAnswer1(categoryId,
questionId);
answer3FromDatabase = myDbHelper.getWrongAnswer2(categoryId,
questionId);
answer4FromDatabase = myDbHelper.getWrongAnswer3(categoryId,
questionId);
myDbHelper.close();
} catch (SQLException sqle) {
System.out.println("Errored out");
try {
throw sqle;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// If the player selects the right answer this method is called to increase
// the score by 10 points
void increaseScore() {
currentScore = currentScore + 10;
com.triviagame.Trivia_Game.finalScore = currentScore;
}
// If the player selects the wrong answer this method is called to decrease
// the score by 10 points
void decreaseScore() {
currentScore = currentScore - 10;
// score can't go below 0
if (currentScore < 0) {
currentScore = 0;
}
com.triviagame.Trivia_Game.finalScore = currentScore;
}
// Displays the current scores on the activity
void displayScore() {
TextView yourScore = (TextView) findViewById(R.id.yourscore);
String currentScoreText;
currentScoreText = Integer.toString(currentScore);
yourScore.setText(currentScoreText);
}
void resetTimer() {
if (mCountDown != null) {
mCountDown.cancel();
}
final TextView myCounter = (TextView) findViewById(R.id.tv);
mCountDown = new CountDownTimer(21000, 1000) {
#Override
public void onFinish() {
questionCheck();
resetTimer();
}
#Override
public void onTick(long millisUntilFinished) {
myCounter.setText("Time left: "
+ String.valueOf(millisUntilFinished / 1000));
}
}.start();
}
void cancelTimer() {
if (mCountDown != null) {
mCountDown.cancel();
}
}
void questionCheck() {
if (nextOrFinishValue == 1) {
Intent e = new Intent(this, finalscore.class);
startActivity(e);
cancelTimer();
}
if (nextOrFinishValue == 0) { // If 10 questions have not been displayed
// yet, display the next question
gatherInfoBeforePopulating();
displayScore();
populateWithQuestionInfo(displayedQuestionFromDatabase,
displayedAnswer1FromDatabase, displayedAnswer2FromDatabase,
displayedAnswer3FromDatabase, displayedAnswer4FromDatabase,
nextOrFinishValue);
}
}
// gathers question and answers from database
void gatherInfoBeforePopulating() {
categoryId = Trivia_Game.getCategoryIdInMainMenu();
// Loop until a valid questionId that hasn't been used is obtained
while (validQuestionNumber == false) {
randomValue0to29 = RandomGenerator0To29();
System.out.println("random30 in onClick = " + randomValue0to29);
alreadyDisplayQuestion = questionsAlreadyDisplayed(randomValue0to29);
if (alreadyDisplayQuestion == true) {
System.out
.println("question number already displayed looking for a non displayed question");
}
if (alreadyDisplayQuestion == false) {
System.out.println("question not displayed yet");
validQuestionNumber = true;
}
}
validQuestionNumber = false;
alreadyDisplayQuestion = false;
questionId = randomValue0to29; // sets the valid random generated number
// to the questionID
// connect to database to gather the question and answers
getInfoFromDatabase(categoryId, questionId);
// Calls random number from 0 to 3 to determine which button will
// display the correct answer
randomValueZeroToThree = RandomGeneratorZeroToThree();
System.out.println("random4 in onClick = " + randomValueZeroToThree);
// Sets the order according to the button that is to display the correct
// answer
switch (randomValueZeroToThree) {
case 0:
displayedAnswer1FromDatabase = answer1FromDatabase; // correct
// answer
displayedAnswer2FromDatabase = answer2FromDatabase;
displayedAnswer3FromDatabase = answer3FromDatabase;
displayedAnswer4FromDatabase = answer4FromDatabase;
rightButton1 = true;
rightButton2 = false;
rightButton3 = false;
rightButton4 = false;
break;
case 1:
displayedAnswer2FromDatabase = answer1FromDatabase; // correct
// answer
displayedAnswer1FromDatabase = answer2FromDatabase;
displayedAnswer3FromDatabase = answer3FromDatabase;
displayedAnswer4FromDatabase = answer4FromDatabase;
rightButton1 = false;
rightButton2 = true;
rightButton3 = false;
rightButton4 = false;
break;
case 2:
displayedAnswer3FromDatabase = answer1FromDatabase; // correct
// answer
displayedAnswer1FromDatabase = answer2FromDatabase;
displayedAnswer2FromDatabase = answer3FromDatabase;
displayedAnswer4FromDatabase = answer4FromDatabase;
rightButton1 = false;
rightButton2 = false;
rightButton3 = true;
rightButton4 = false;
break;
case 3:
displayedAnswer4FromDatabase = answer1FromDatabase; // correct
// answer
displayedAnswer1FromDatabase = answer2FromDatabase;
displayedAnswer3FromDatabase = answer3FromDatabase;
displayedAnswer2FromDatabase = answer4FromDatabase;
rightButton1 = false;
rightButton2 = false;
rightButton3 = false;
rightButton4 = true;
break;
default:
displayedAnswer1FromDatabase = answer4FromDatabase; // no correct
// answer
displayedAnswer2FromDatabase = answer2FromDatabase;
displayedAnswer3FromDatabase = answer3FromDatabase;
displayedAnswer4FromDatabase = answer1FromDatabase;
rightButton1 = false;
rightButton2 = false;
rightButton3 = false;
rightButton4 = false;
}
// After the 9th question is displayed, the nextOfFinishValue should be
// set to 1 so the button displayes Finish instead of Next
if (numberOfQuestionsDisplayedCounter < 9) {
nextOrFinishValue = 0;
} else {
nextOrFinishValue = 1;
}
}
// Listeners for the 4 buttons with answers and the Next or Finish buttons
public void onClick(View v) {
switch (v.getId()) {
case R.id.answer1button:
if (rightButton1 == true) {
increaseScore();
resetTimer();
// put here to change color of button
} else {
decreaseScore();
resetTimer();
// change button to red
}
questionCheck();
break;
case R.id.answer2button:
if (rightButton2 == true) {
increaseScore();
resetTimer();
} else {
decreaseScore();
resetTimer();
}
questionCheck();
break;
case R.id.answer3button:
if (rightButton3 == true) {
increaseScore();
resetTimer();
} else {
decreaseScore();
resetTimer();
}
questionCheck();
break;
case R.id.answer4button:
if (rightButton4 == true) {
increaseScore();
resetTimer();
} else {
decreaseScore();
resetTimer();
}
questionCheck();
break;
}
}
#Override
protected void onResume() {
super.onResume();
music.play(this, R.raw.musicbackground); // play background music
}
#Override
protected void onPause() {
super.onPause();
music.stop(this); // stop playing background music
}
}
mCountdown is never null so change your code
void cancelTimer() {
if (mCountDown != null) {
mCountDown.cancel();
}
}
To
void cancelTimer() {
mCountDown.cancel();
}

CountDownTimer trivia game score - Android (java)

I have a trivia game that displays 10 questions and they each have a 10 second timer. I have 2 problems that do not function correctly.
Firstly, If the timer runs out on a question, it displays the next question but the timer does not reset. The textviews stay at "Time's up!" and "Time Elapsed: 10000" instead of restarting the timer on the new question that is displayed.
Lastly, on the Results page the correct score is not displayed in the textview. The percentage textview displays correctly but the score textview displays "android.widget.TextView#416473c" or some other random memory location.
The program never crashes just functions incorrectly. Any code structure or other suggestions is much appreciated! This is my first android mobile app attempt and I am slowly and strugglingly through it. Yet enjoying it! :)
QuesteionView.java
public class QuestionView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);
answer1 = (Button)findViewById(R.id.answer1);
answer2 = (Button)findViewById(R.id.answer2);
answer3 = (Button)findViewById(R.id.answer3);
answer4 = (Button)findViewById(R.id.answer4);
question = (TextView)findViewById(R.id.question);
queries = getIntent().getParcelableArrayListExtra("queries");
timer = (TextView)findViewById(R.id.timer);
timeElapsedView = (TextView)findViewById(R.id.timeElapsedView);
cdTimer = new Timer(startTime, interval);
loadQuestion();
}
public void loadQuestion() {
if(i == 9) {
endQuiz();
} else {
if(!timerHasStarted) {
cdTimer.start();
timerHasStarted = true;
} else {
cdTimer.cancel();
timerHasStarted = false;
}
answer = queries.get(i).getCorrectAnswer();
question.setText(queries.get(i).getQuery());
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
queries.get(i).setSelectedAnswer(0);
if(answer == 0) {
correctAnswers++;
nextQuestion();
} else {
wrongAnswers++;
nextQuestion();
}
}
});
//same type of code for buttons for answers 2 through 4.
}
}
public void nextQuestion() {
score = score + timeElapsed;
i++;
loadQuestion();
}
public class Timer extends CountDownTimer {
public Timer(long startTime, long interval) {
super(startTime, interval);
}
public void onFinish() {
if(i == 9) {
cdTimer.cancel();
} else {
timer.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
wrongAnswers++;
nextQuestion();
}
}
public void onTick(long millisUntilFinished) {
timer.setText("Time remain: " + Long.toString(millisUntilFinished));
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + Long.toString(timeElapsed));
}
}
public void endQuiz() {
Intent intent = new Intent(QuestionView.this, Results.class);
intent.putExtra("correctAnswers", correctAnswers);
intent.putExtra("wrongAnswers", wrongAnswers);
intent.putExtra("score", score);
intent.putParcelableArrayListExtra("queries", queries);
startActivity(intent);
}
}
Results.java
public class Results extends Activity {
QuestionView qv = new QuestionView();
ArrayList<Question> queryList = qv.getQueries();
int cAnswers;
int wAnswers;
long score;
ArrayList<Question> qs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultsmain);
cAnswers = getIntent().getIntExtra("correctAnswers", -1);
wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
score = getIntent().getLongExtra("score", -1);
qs = getIntent().getParcelableArrayListExtra("queries");
Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn);
mainmenuBtn.setText("Main Menu");
mainmenuBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
restart();
}
});
showResults();
}
public void showResults() {
ArrayList<TextView> tList = new ArrayList<TextView>(9);
TextView header = (TextView)findViewById(R.id.header);
header.setText("SUMMARY");
TextView percentage = (TextView)findViewById(R.id.percentage);
percentage.setText(Integer.toString(10 * cAnswers) + "%");
TextView score = (TextView)findViewById(R.id.score);
String s = "" + score;
score.setText(s);
TextView q1 = (TextView)findViewById(R.id.q1);
TextView q2 = (TextView)findViewById(R.id.q2);
TextView q3 = (TextView)findViewById(R.id.q3);
TextView q4 = (TextView)findViewById(R.id.q4);
TextView q5 = (TextView)findViewById(R.id.q5);
TextView q6 = (TextView)findViewById(R.id.q6);
TextView q7 = (TextView)findViewById(R.id.q7);
TextView q8 = (TextView)findViewById(R.id.q8);
TextView q9 = (TextView)findViewById(R.id.q9);
TextView q10 = (TextView)findViewById(R.id.q10);
tList.add(q1);
tList.add(q2);
tList.add(q3);
tList.add(q4);
tList.add(q5);
tList.add(q6);
tList.add(q7);
tList.add(q8);
tList.add(q9);
tList.add(q10);
for(int i = 0; i < tList.size(); i++) {
tList.get(i).setText(qs.get(i).getQuery());
if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) {
tList.get(i).setTextColor(Color.GREEN);
} else {
tList.get(i).setTextColor(Color.RED);
}
}
}
public void restart() {
Intent intent = new Intent(Results.this, MainMenu.class);
startActivity(intent);
}
}
From all of that code, this is what I think is happening
Firstly, If the timer runs out on a question, it displays the next question but the timer does not reset. The textviews stay at "Time's up!" and "Time Elapsed: 10000" instead of restarting the timer on the new question that is displayed.
This appears to be due to you not setting your timerHasStarted variable to false after the time runs out so I would set that to false probably when you load your next question or after you show the results.
Lastly, on the Results page the correct score is not displayed in the textview. The percentage textview displays correctly but the score textview displays "android.widget.TextView#416473c" or some other random memory location.
This is because you are setting your q variables to the textview and getting the id. You need something like q1.getText().toString()
You have multiple variables with the same name score. So change it to
TextView score2 = (TextView)findViewById(R.id.score);
String s = "" + score;
score2.setText(s);
the score displays not what you expected because you assigned the String s = "" + score where score is what you named for the Textview which obviously not an integer and not equivalent to the score that the user has. :)

Categories

Resources