Try to continue quiz after wrong answer - android

Demo picture
I try to make a firebase quiz application project.It work's good but when i press the wrong button then it was hang and didn't work for next.
my code is here:
#Override
public void onClick(View v) {
mCountDown.cancel();
//still have question in list
if (index < totalQuestion) {
Button clickedButton = (Button) v;
if (clickedButton.getText().equals(Common.list_question.get(index).getCorrectAnswer())) {
score += 10;
correctAnwer++;
showQuestion(++index);
}
txtScore.setText(String.format("%d", score));
}
}
After right answer it will go this method:
private void showQuestion(int index) {
if (index < totalQuestion) {
thisQuestion++;
txtQuestionNum.setText(String.format("%d / %d", thisQuestion, totalQuestion));
progressBar.setProgress(0);
progressValue = 0;
if (Common.list_question.get(index).getIsImageQuestion().equals("true")) {
Picasso.with(getBaseContext())
.load(Common.list_question.get(index).getQuestion())
.into(question_image);
question_image.setVisibility(View.VISIBLE);
question_text.setVisibility(View.INVISIBLE);
} else {
question_text.setText("Q. "+Common.list_question.get(index).getQuestion());
question_image.setVisibility(View.INVISIBLE);
question_text.setVisibility(View.VISIBLE);
}
btnA.setText(Common.list_question.get(index).getAnswerA());
btnB.setText(Common.list_question.get(index).getAnswerB());
btnC.setText(Common.list_question.get(index).getAnswerC());
btnD.setText(Common.list_question.get(index).getAnswerD());
mCountDown.start();
} else {
Intent intent = new Intent(this, Done.class);
Bundle dataSend = new Bundle();
dataSend.putInt("SCORE", score);
dataSend.putInt("TOTAL", totalQuestion);
dataSend.putInt("CORRECT", correctAnwer);
intent.putExtras(dataSend);
startActivity(intent);
finish();
}
}
But after wrong answer it didn't work.I need to a condition for that but don't know how can i write code for else condition

If I understand correctly, this logic checks if the user has clicked the correct answer button:
if (clickedButton.getText().equals(Common.list_question.get(index).getCorrectAnswer())) {
score += 10;
correctAnwer++;
showQuestion(++index);
}
Now, you only want the score and number of correct answers (correctAnwer) to change if the correct answer is picked (i.e. go inside the if statement), but you always want to show the next question regardless of if the answer is correct or not. The easiest way is just to move showQuestion outside of the if statement like so:
if (clickedButton.getText().equals(Common.list_question.get(index).getCorrectAnswer())) {
score += 10;
correctAnwer++;
}
showQuestion(++index);
txtScore.setText(String.format("%d", score));
Though if you do want to perform additional actions on an incorrect answer before moving to the next question, you can use an else statement like so:
if (clickedButton.getText().equals(Common.list_question.get(index).getCorrectAnswer())) {
score += 10;
correctAnwer++;
showQuestion(++index);
} else {
// Do stuff that only needs to be done for an incorrect answer
showQuestion(++index);
}
txtScore.setText(String.format("%d", score));

Related

Android WebView : Navigate to Url Hash without reloading using goBackOrForward

I want to navigate Angular single page application without reloading the webpage. On button click I'm calling following code to navigate.
String fullUrl = "https://example.com/page1";
String hashUrl = "/page1";
public void goBackInWebView(String fullUrl, String hashUrl) {
WebBackForwardList history = webview.copyBackForwardList();
int index;
int currentIndex = history.getCurrentIndex();
for (int i = 0; i < history.getSize(); i++) {
index = i - currentIndex;
if ((webview.canGoBackOrForward(1 + i)) || (webview.canGoBackOrForward(1 - i))) {
if (history.getItemAtIndex(i).getUrl().endsWith(hashUrl)) {
webview.goBackOrForward(index);
break;
} else if (i == history.getSize()) {
webview.loadUrl(fullUrl);
break;
}
} else {
//OUTER ELSE
webview.loadUrl(fullUrl);
break;
}
}
}
If the webpage is not saved in the history it will call webview.loadUrl(fullUrl); else it will load the page using webview.goBackOrForward(index);.
but is above code everytime OUTER ELSE is callling.
First of all canGoBackOrForward is used for Getting whether the page can go back or forward the given number of steps. as you mentioned in question your web page is single page so you don't need call this method. I think bellow code can solve your problem.
public void goBackInWebView(String fullUrl, String hashUrl) {
WebBackForwardList history = webview.copyBackForwardList();
if (history.getSize() == 0) {
webview.loadUrl(fullUrl);
}else {
for (int i = 0; i < history.getSize(); i++) {
if (history.getItemAtIndex(i).getUrl().endsWith(hashUrl)) {
webview.goBackOrForward(i);
break;
} else {
webview.loadUrl(fullUrl);
}
}
}
}
I find it difficult to see much relation to Android and WebView, because the problem is SPA.
Better use hashtag # navigation for SPA, because this won't request anything server-side.
The point simply is, that the back button does not matter the least, while never navigating.
One just needs to bind JavaScript, which runs whenever window.location.hash changes.

how to make sure a button function is executed properly

I want to make a button that allows to go to a random page, but I am having trouble with the logic of it, as I can see loopholes but I dont know how to solve them. Or should i use a different approach? Code as below.
qn3_nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i=1; i<4; i++) {
int rng = new Random().nextInt(2) + 1;
if (rng == i && !qns.contains(1)) {
qns.add(1);
Intent qn1 = new Intent(question3.this, question1.class);
qn1.putExtra("name", name);
qn1.putIntegerArrayListExtra("questions", qns);
startActivity(qn1);
} else if (rng == i && !qns.contains(2)) {
qns.add(2);
Intent qn2 = new Intent(question3.this, question2.class);
qn2.putExtra("name", name);
qn2.putIntegerArrayListExtra("questions", qns);
startActivity(qn2);
} else if (rng == i && !qns.contains(3)) {
qns.add(3);
Intent qn3 = new Intent(question3.this, question3.class);
qn3.putExtra("name", name);
qn3.putIntegerArrayListExtra("questions", qns);
startActivity(qn3);
}
}
}
});
loopshole that is... the user will need to press the button multiple times if the function does not meet the conditions
First Add your Intents into an ArrayList<Intent>:
ArrayList<Intent> pages = new ArrayList();
pages.add(qn1);
pages.add(qn2);
pages.add(qn3);
After that generate a random int between 0 and the list.size() inside the onClick and get that page from the list:
int index = new Random().nextInt(pages.size());
Intent page = pages.get(index);
startActivity(page);
Finally remove that page from the list by calling:
pages.remove(index);
Just don't forget to check if the ArrayList is empty or not in the first line of the onClick:
if(pages.size == 0) return;
I hope it works!

Use arraylist for multiple if statements (with buttons)

I'm currently working on a school project in Android Studio and so far I've written a code which generates random equations.
Now I display two equations on the screen and the user then has to decide wether the second equation is bigger or smaller than the first one. If the second is bigger, the user presses the button 'bigger', if the second one is smaller, the user presses the button with 'smaller'.
Now I'm trying to write the code that if the user pressed correct, it generates a new equation, if he was wrong, the process stops.
I was thinking of if statements like so:
final ArrayList<String> arrayListCheck = new ArrayList<String>();
if(doubleAnswer1 > doubleAnswer2){
arrayListCheck.add("smaller");
} else {
arrayListCheck.add("bigger");
}
final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger)){
arrayListCheck.add("bigger");
} else {
arrayListCheck.add("smaller");
}
}
};
buttonBigger.setOnClickListener(listener);
buttonSmaller.setOnClickListener(listener);
In the arraylist arrayListCheck it will store either 'bigger' or 'smaller'. Now I want to check if the elements in the arraylist are both the same (either both 'bigger' or 'smaller'), if so a new equation will be generated. If the elements in the arraylist are different (not both the same), the process will be stopped.
I don't know if that really works, so it would be nice if someone could help me with this.
If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)
Thank you already in advance for your help!
I wouldn't use an ArrayList for that.
I would do the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
Log.v("TAG", "you are right");
} else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
Log.v("TAG", "you are right");
} else {
Log.v("TAG", "you are wrong");
}
}
};
Arrays are not really neccessary for this kind of simple comparison.

Show correct answer in multiple choice app [duplicate]

This question already has answers here:
How to change color of multiple choice to show correct answer
(2 answers)
Closed 9 years ago.
Im doing a multiple choice apps with 4 choices. I've downloaded a code. But how to prompt the user of the correct answer if his answer is incorrect answer (at the same time).Here is a sample of the code. Correct answer is green. Wrong answer is red.
optionOne.setOnClickListener(this); //On First Option selection
optionTwo.setOnClickListener(this); //On Second Option selection
optionThree.setOnClickListener(this); //On Third Option selection
optionFour.setOnClickListener(this); //On Forth Option selection
public void onClick(View v) {
if(v.getId() == optionOne.getId()){
onOptionSelected(optionOne.getText().toString(), v);
optionOne.setBackgroundColor(Color.RED);
}else if(v.getId() == optionTwo.getId()){
onOptionSelected(optionTwo.getText().toString(), v);
optionTwo.setBackgroundColor(Color.RED);
}else if(v.getId() == optionThree.getId()){
onOptionSelected(optionThree.getText().toString(), v);
optionThree.setBackgroundColor(Color.RED);
}else if(v.getId() == optionFour.getId()){
onOptionSelected(optionFour.getText().toString(), v);
optionFour.setBackgroundColor(Color.RED);
}else if(v.getId() == pause.getId()){ //True when Game is Paused
//When an option of a question is selected
private void onOptionSelected(String option, View v){
if(!isGamePaused && !isGameEnded) { //true when game is being played
ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
correct += 1;
v.setBackgroundColor(Color.GREEN);
}
else{
incorrect += 1;
totalPoints -= pointsPerWrongAnswer;
}
I need to insert a code on this portion to show the correct answer in green background if the user answer is incorrect but i don't know how.
else{
incorrect += 1;
totalPoints -= pointsPerWrongAnswer;
The database questions is in .plist
<question>
<key>Question</key>
<string>What is the ....</string>
<key>Options</key>
<array>
<string>option 1</string>
<string>option 2</string>
<string>option 3</string>
<string>option 4</string>
</array>
<key>Answer</key>
<integer>2</integer>
</question>
Here is my other code
public ATriviaQuestion(){
isThisQuestionAsked = false;
answer = -1;
answered = "";
}
public String GetQuestion()
{ return this.question; }
public void SetQuestion(String _question)
{ this.question=_question; }
public ArrayList<String> GetOptions()
{ return this.options; }
public void SetOptions(ArrayList<String> _options)
{ this.options = _options; }
public int GetAnswer()
{ return this.answer; }
public void SetAnswer(int _answer)
{ this.answer = _answer; }
I'm having trouble following exactly what is what here but you can loop through your options until the text equals tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1) (which appears to be how you are getting the correct answer) then set that View's background color to green.

android unable to click button after return

I have a view to create an account. If the save button is clicked and any of the fields are left open, it displays a toast. If all fields are filled in, the account is saved. I tried to accomplish this with an onClickListener that has an iteration through all the fields. It works perfectly if a field is not filled in and it works perfectly if alle fields are filled, but when a field isn't filled, I type something in there, try to save again and the button doesn't do anything.
I think it has something to do with the return, but I don't know what to do else. If the return wouldn't be there, I would get a toast for each field that isn't filled in.
Here's the relevant code:
private void registerButtonListeners() {
mCRUDAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mEditTexts.length; i++) {
if(mEditTexts[i].getText().length() == 0){
CommonCode.showToast(mNoTextTitles[i], mContext, mViewGroup);
mEmptyField = 1;
return;
}
};
if (mEmptyField == 0){
saveState();
}
}
});
}
thanks guys!
You're never resetting your flag back to 0!
so...
#Override
public void onClick(View v) {
mEmptyField = 0;//RIGHT HERE (give them the benefit of the doubt)
for (int i = 0; i < mEditTexts.length; i++) {
if(mEditTexts[i].getText().length() == 0){
CommonCode.showToast(mNoTextTitles[i], mContext, mViewGroup);
mEmptyField = 1; //You were too optimistic, they failed.
return;
}
};
if (mEmptyField == 0){
saveState();
}
}
});
Now, you're doing this test for the first time, every time. Otherwise, you go through and set that flag to 1, and next time, even though your loop never finds a match, when you get to the if mPentyField == 0 test, it fails cause you set that to 1 in the previous go around.

Categories

Resources