I need to take the three strings and make sure that they are the same and that they are not null or empty.
This code is working fine in a Java online compiler but not in Android Studio.
int i = 0; ...
B1 = (Button) findViewById(R.id.B1);
B2 = (Button) findViewById(R.id.B2);
B3 = (Button) findViewById(R.id.B3);
B1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if (i==0){
B1.setText("X");
i++;}
else if (i==1){
B1.setText("O");
i = 0;}
}
});
// the same for B2 and B3...
String SB1 = B1.getText().toString();
String SB2 = B2.getText().toString();
String SB3 = B3.getText().toString();
if (SB1.equals(SB2) && SB2.equals(SB3) && !SB1.equals("") ){
Win.setText("win");
}
If you want to make sure that two strings are same or not...
String one = "RAMESH";
String two = "RAMESH";
if((one.contentEquals(two))
{
Toast.makeText(getActivity(), "Strings are same", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getActivity(), "Strings are NOT SAME", Toast.LENGTH_SHORT).show();
}
You can check in your case like :
if(SB1.contentEquals(SB2) && SB2.contentEquals(SB3))
{
if(!SB1.equals(""))
{
Win.setText("win");
}
}
Hope it helps
Check the condition as
String SB1 = B1.getText().toString().trim();
String SB2 = B2.getText().toString().trim();
String SB3 = B3.getText().toString().trim();
if(SB1.equals(SB2) && SB2.equals(SB3) && !SB1.isEmpty()){
Win.setText("win");
}
But your condition getting false because, all buttons having empty text / no text and it get false due to SB1.isEmpty() and when you performs click on B1 button, it changes the text on it, which does not match condition for equality of string. So make sure to change text of B2 and B3 buttons too.
Related
hello guys im newbie in android i want just to change the text view text if the caculate value between 18 or 25 i used if statment but there is no results
any idea sometimes if i removed one of the >its worked but only for biggest value
public class GB extends ActionBarActivity {
ImageView im1;
EditText mEditText1,mEditText2;
TextView mTextView,m5;
Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_GB);
im1 = (ImageView)findViewById(R.id.imageView1);
mEditText1 = (EditText)findViewById(R.id.editText1);
mEditText2 = (EditText)findViewById(R.id.editText2);
mTextView = (TextView)findViewById(R.id.textView4);
m5 = (TextView)findViewById(R.id.textView5);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
}
String word2 = mEditText2.getText().toString();
String word = mEditText1.getText().toString();
if (word.trim().equals("")){
mEditText1.setError("Insert Hight");
}
else if (word2.trim().equals("")){
mEditText2.setError("Insert Wight");
} else{
calculate();
}
}
});
}
public void calculate(){
Double value1 = Double.parseDouble(mEditText1.getText().toString());
Double value2 = Double.parseDouble(mEditText2.getText().toString());
Double calculatedValue = value1/(value2*value2)*10000;
calculatedValue = Math.round(calculatedValue*10)/10.0d;
mTextView.setText(calculatedValue.toString());
if (mTextView.getText().toString().trim().length() <10 && mTextView.getText().toString().trim().length() >18) {
im1.setBackgroundResource(R.drawable.s1);
m5.setText("low");
} else if (mTextView.getText().toString().trim().length() <18 && mTextView.getText().toString().trim().length() >25) {
im1.setBackgroundResource(R.drawable.s3);
m5.setText("Best");
} else if (mTextView.getText().toString().trim().length() <25 && mTextView.getText().toString().trim().length() >100) {
im1.setBackgroundResource(R.drawable.s3);
m5.setText("over");
}
}
}
An integer value cannot be smaller than 18 and bigger than 25 at the same time.
Instead of
if (mTextView.getText().toString().trim().length() <18 && mTextView.getText().toString().trim().length() >25)
Use this one if you want to check a value between 18 and 25.
if (mTextView.getText().toString().trim().length() > 18 && mTextView.getText().toString().trim().length() < 25)
If you want to check if a value is in an interval, you have to write it as follows:
if(myvalue > lowerbound && myvalue < upperbound )
The following code probably does what you want.
public void calculate(){
Double value1 = Double.parseDouble(mEditText1.getText().toString());
Double value2 = Double.parseDouble(mEditText2.getText().toString());
Double calculatedValue = value1/(value2*value2)*10000;
calculatedValue = Math.round(calculatedValue*10)/10.0d;
mTextView.setText(calculatedValue.toString());
if (calculatedValue >= 10 && calculatedValue < 18) {
im1.setBackgroundResource(R.drawable.s1);
m5.setText("low");
} else if (calculatedValue >= 18 && calculatedValue < 25) {
im1.setBackgroundResource(R.drawable.s3);
m5.setText("Best");
} else if (calculatedValue >= 25 && calculatedValue < 100) {
im1.setBackgroundResource(R.drawable.s3);
m5.setText("over");
}
}
Your switch statement here is useless, You have already set click listener to button and again you are trying to check if it's the same button clicked.
It would good if you check if the value entered is numeric.
when you use if, else if and else - one and only one will be executed, which ever full fills the statement will be executed and rest of the code won't execute, say for instance have a look at the following:
if( a > b){
//show A is bigger than b;
} else if ( a == b){
// show A equals to b
} else{
// b > a
}
Here,
if (word.trim().equals("")){//if this conditions works, code won't go to test else if or else statement.
mEditText1.setError("Insert Hight");
}
else if (word2.trim().equals("")){
mEditText2.setError("Insert Wight");
} else{
calculate();
}
The way you are checking the values is wrong, the statement will be always false, as 1. you are testing if the value if < 10 and at the same time you are test if the value is > 18, one of which be false for sure.
if(mTextView.getText().toString().trim().length() <10 && mTextView.getText().toString().trim().length() >18)
I want to make a Hint button, so when I click on it, I want to delete two buttons from the list (answers list). Now I don't know how to do it,ho w to make the for loop on the button array, so I can make this buttons invisible.
public class ClassicMode extends Activity {//מהמשחק עצמו
String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
Button hint;
TextView guess;
TextView numOfGuess;
TextView score;
TextView scorenum;
DatabaseHandler db = new DatabaseHandler(this);
String fn;
Guesses G;
Bitmap bm;
Score s;
Button [] b = new Button[4];
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
score =(TextView)findViewById(R.id.score);
scorenum =(TextView)findViewById(R.id.scorenum);
scorenum.setText(String.valueOf(s.score));
guess =(TextView)findViewById(R.id.guesses);
numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
hint =(Button)findViewById(R.id.hint);
Flags f = new Flags();
Random r = new Random();//הדגל שיבחר לשאלה
int num = r.nextInt(160);//Up
f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
fn = f.getName().toString();
pic = f.getImage().toString();
pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק
//מערך ארבע כפתורים כנגד ארבע תשובות
b[0] = (Button)findViewById(R.id.button1);
b[1] = (Button)findViewById(R.id.button2);
b[2] = (Button)findViewById(R.id.button3);
b[3] = (Button)findViewById(R.id.button4);
List<String>Answers=new ArrayList<String>();//מערך תשובות
Answers.add(f.getName().toString());//הוספת התשובה הנכונה
for(int i=1;i<4;i++)
{
num = r.nextInt(200);
String valToAdd1 = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd1)){
Answers.add(valToAdd1);
}
}
/*num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());//הוספת 3 תשובות רנדומליות
num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());
num = r.nextInt(30);
Answers.add(db.getFlag(num).getName().toString());*/
Collections.shuffle(Answers);//ערבוב התשובות
for(int i=0;i<Answers.size();i++)
{
b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
}
}//end of OnCreat
Now what I've done (there is the function check, which check if you answered correctly and the hint which I don't know how to make):
public void check(View v)
{
Log.d("yes", fn);
Button b = (Button)v;
String text = b.getText().toString();
if(text.equals(fn))
{
s.score+=5;
resetQuiz();
}
else
{
s.score-=5;
if(Guesses.numOfGuesses==1)
{
G.setNumOfGuesses(3);
finish();//כאשר מספר הניחושים
return;
}
Guesses.numOfGuesses--;
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
}
}
public void hint(View v)
{
G.numOfGuesses--;
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
{
if()
}
}
Note: this is {mostly} pseudocode
I suggest keeping two separate lists of your answers. Your Flag object already holds the correct answer. You need a list to keep track of the wrong answers (so we don't have to loop and check against each item every time). You also need a list of all of them together that you can shuffle and display.
I took a little bit of liberty making your variable names longer so they are more clear.
onCreate() {
...
btnHint.setOnClickListener(hintOnClickListener);
...
Flag f = db.getFlag(randomNum); // This is the real question & answer
List<String> wrongAnswers = new ArrayList<String>(3);
List<String> allAnswers = new ArrayList<String>(4);
// Loop 3 times for 3 random wrong answers
for (int i=0; i<=3; i++) {
randNum = r.nextInt(200);
String randWrongAnswer = db.getFlag(randNum).getName().toString();
if (! wrongAnswers.contains(randWrongAnswer)) {
wrongAnswers.add(randWrongAnswer);
}
}
allAnswers.add(f.getName().toString());
allAnswers.addAll(wrongAnswers);
Collection.shuffle(allAnswers);
...
}
I like to declare all my listeners separately further down in the code, to keep the OnCreate method clean and legible.
private OnClickListener hintOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
G.numOfGuesses--;
// Since you shuffled the 'allAnswers' before displaying to the screen,
// we can just pick the first 2 answers from wrongAnswers list
// and it will appear to be random to the user.
for (int i=0; i < buttons.length; i++) {
String buttonText = buttons[i].getText().toString();
if (buttonText.equals(wrongAnswers.get(0))
|| buttonText.equals(wrongAnswers.get(1))) {
buttons[i].setVisibility(View.INVISIBLE);
}
}
}
};
Edit: to add hint logic based on OP's comment.
i'm trying to do this feature where i calculate the calories needed in BMIcalculation class, then i created another class called CalorieIntake where in here i'll calculate the total calorie intake. (i do this by extends the BMIcalculation class.)
Then when i click on the 'Check' button, it should compare between this two value and show the interpretation. However i keep getting error at the 'interpretDiff(float diffValue)' part it mentioned it must return with a String value.
Here are my codes..pls help me to check where to problem is. Or is there a better way to do so? pls advice me. Thanks a lot..
public class CalorieIntake extends BMIcalculation {
TextView counter1;
Button compare;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.form_calorieintake2);
Button btn_calcIntake = (Button) findViewById(R.id.button_calcIntake);
btn_calcIntake.setOnClickListener(btnListener_calcIntake);
counter1 = (TextView) findViewById(R.id.textView_totalCalorieIntake);
Button compare = (Button) this.findViewById(R.id.checkIntake);
compare.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
try {
if ((!counter1.equals("")) && (!caloriesresult.equals("")))
{
TextView compareText = (TextView)findViewById(R.id.compareLabel);
EditText counter1E = (EditText) findViewById(R.id.textView_totalCalorieIntake);
EditText caloriesresultE = (EditText)findViewById(R.id.caloriesText);
float calorieIntake = Float.parseFloat(counter1E.getText().toString().trim());
float calorieNeed = Float.parseFloat(caloriesresultE.getText().toString().trim());
float diffValue = calDiff(calorieIntake, calorieNeed);
String calInterpretation = interpretDiff(diffValue);
compareText.setText("Difference of" + diffValue + " : " + calInterpretation);
}
}catch (Exception k)
{ System.out.println(k);
Toast.makeText(CalorieIntake.this, "Error", Toast.LENGTH_SHORT).show();
}
}
private String interpretDiff(float diffValue)
{
if (diffValue < 100)
{
return "Eat more";
}
}
private float calDiff(float calorieIntake, float calorieNeed) {
return (float) (calorieIntake - calorieNeed);
}
});
}
In interpretDiff, what happens if diffValue is not less than 100? You return nothing. However, java requires that you return something, as that is what the declaration of the method implies.
A solution would be:
private String interpretDiff(float diffValue)
{
if (diffValue < 100)
return "Eat more";
return "Eat less";
}
The else statement for if diffValue is not less than 100 is not necessary here because the second return statement is only called if diffValue >= 100 (since hitting a return statement exits out of the method).
With regards to the issue of the exception being thrown, and the Toast error being called, that can only happen as a result of the following code:
TextView compareText = (TextView)findViewById(R.id.compareLabel);
EditText counter1E = (EditText) findViewById(R.id.textView_totalCalorieIntake);
EditText caloriesresultE = (EditText)findViewById(R.id.caloriesText);
float calorieIntake = Float.parseFloat(counter1E.getText().toString().trim());
float calorieNeed = Float.parseFloat(caloriesresultE.getText().toString().trim());
float diffValue = calDiff(calorieIntake, calorieNeed);
String calInterpretation = interpretDiff(diffValue);
compareText.setText("Difference of" + diffValue + " : " + calInterpretation);
Assuming findViewById doesn't have an issue in it, the error is probably coming from getting the text of counter1E, caloriesresultE (if they are null). If neither of those are null, then check to make sure callDiff and interpretDiff don't have bugs in them. Finally, make sure compareText is not null.
i'm now in java and eclipse, i just tried to build a simple calculator, but i can't make it to return with the result.
My plan was basicly that when the user click one of the operators, and the EditText isn't empty, then First variable will be equal with the EditText, and the Operator variable change, and when the user hit Result button, and the First variable isn't empty, then the the EditText will be equal with the Second variable, and the Result variable will be equal with the result basicly. I think i messed up something with the types and variables, but i can't figure it out what exactly.
Could someone help?
Here is the java code;
public class Main extends Activity implements OnClickListener {
LinearLayout linear;
float First, Second, Operator, Result;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bResult, bTizedes, bSzorzas, bKivonas, bOsztas, bOsszeadas;
EditText eT;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
First = 0;
Second = 0;
Operator = 0;
Result = 0;
b0 = (Button) findViewById(R.id.b0);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
bTizedes = (Button) findViewById(R.id.bTizedes);
bSzorzas = (Button) findViewById(R.id.bSzorzas);
bResult = (Button) findViewById(R.id.bEgyenlo);
bKivonas = (Button) findViewById(R.id.bKivonas);
bOsztas = (Button) findViewById(R.id.bOsztas);
bOsszeadas = (Button) findViewById(R.id.bOsszeadas);
eT = (EditText) findViewById(R.id.eT);
b0.setOnClickListener(this);b1.setOnClickListener(this);b2.setOnClickListener(this);b3.setOnClickListener(this);
b4.setOnClickListener(this);b5.setOnClickListener(this);b6.setOnClickListener(this);b7.setOnClickListener(this);
b8.setOnClickListener(this);b9.setOnClickListener(this);bTizedes.setOnClickListener(this);bSzorzas.setOnClickListener(this);
bResult.setOnClickListener(this);bKivonas.setOnClickListener(this);bOsztas.setOnClickListener(this);bOsszeadas.setOnClickListener(this);
bSzorzas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 2;
eT.setText(null);
}
else if(First != 0)
{
Operator = 2;
};
}
});
bKivonas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 4;
eT.setText(null);
}
else if(First != 0)
{
Operator = 4;
};
}
});
bOsztas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 1;
eT.setText(null);
}
else if(First != 0)
{
Operator = 1;
};
}
});
bOsszeadas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 3;
eT.setText(null);
}
else if(First != 0)
{
Operator = 3;
};
}
});
bResult.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First != 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float Second = Float.valueOf(eT.getText().toString());
if(Operator == 1){
int Result = (int) (First) / (int) (Second);
eT.setText(Result);
}
else if(Operator == 2){
int Result = (int) (First) * (int) (Second);
eT.setText(Result);
}
else if(Operator == 3){
int Result = (int) (First) + (int) (Second);
eT.setText(Result);
}
else if(Operator == 4){
int Result = (int) (First) - (int) (Second);
eT.setText(Result);
}
eT.setText(null);
}
else if(First == 0)
{
};
}
});
}
public void onClick(View v) {
switch(v.getId()){
case R.id.b0:
eT.setText( eT.getText() + "0");
break;
case R.id.b1:
eT.setText( eT.getText() + "1");
break;
case R.id.b2:
eT.setText( eT.getText() + "2");
break;
case R.id.b3:
eT.setText( eT.getText() + "3");
break;
case R.id.b4:
eT.setText( eT.getText() + "4");
break;
case R.id.b5:
eT.setText( eT.getText() + "5");
break;
case R.id.b6:
eT.setText( eT.getText() + "6");
break;
case R.id.b7:
eT.setText( eT.getText() + "7");
break;
case R.id.b8:
eT.setText( eT.getText() + "8");
break;
case R.id.b9:
eT.setText( eT.getText() + "9");
break;
}
}}
Try to focus on this Tutorial and see how things are done, Also try to improve it
float First = Float.valueOf(eT.getText().toString());
2 problems:
Why re-declare your variable First here? Simply use it.
To parse, use Float.parseFloat(eT.getText().toString());
And why do you name your variables as First? use first.Follow conventions.
EDIT:
Could see the same issue of re-declaring almost all your variables. Please don't do that.
I made a simple quiz game for android, right now there's only 10 questions, and 40 answers. (4 answers for each question)
Sometimes when I hit a button it gives me more than one correct answer at a time, also it might even do it when answer's not correct.
Any idea what's wrong in this code? Cause Im not calling the isCorrect boolean method multiple times (in one click).
Code:
public class ETBetaActivity extends Activity implements View.OnClickListener {
Button answer_1,
answer_2,answer_3,
answer_4,main;
TextView q_textview,
tip;
private String a1,a2,a3,a4 = "";
private int i1 = 0;
public static int correct = 0;
private boolean alive = true;
MediaPlayer button_click;
private String[] questions =
{"Q1",
"Q2",
"Q3",
"Q4",
"Q5", //5
"Q6",
"Q7",
"Q8",
"Q9",
"Q10" //10
};
public static int question_amount = 10;
private String[] answers_correct =
{"Correct answer - 1",
"Correct answer - 2",
"Correct answer - 3",
"Correct answer - 4",
"Correct answer - 5",
"Correct answer - 6",
"Correct answer - 7",
"Correct answer - 8",
"Correct answer - 9",
"Correct answer - 10"
};
private String[][] answers_wrong =
{ {"Q1-1", "Q1-2" , "Q1-3"},
{"Q2-1", "Q2-2" , "Q2-3"},
{"Q3-1", "Q3-2" , "Q3-3"},
{"Q4-1", "Q4-2" , "Q4-3"},
{"Q5-1", "Q5-2" , "Q5-3"},
{"Q6-1", "Q6-2" , "Q6-3"},
{"Q7-1", "Q7-2" , "Q7-3"},
{"Q8-1", "Q8-2" , "Q8-3"},
{"Q9-1", "Q9-2" , "Q9-3"},
{"Q10-1", "Q10-2" , "Q10-3"}
};
List<String> question_list = new ArrayList<String>();
List<String> answer_list_correct = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getData();
Game(i1);
}
#Override
public void onClick(View view) {
if (alive == false) {
// startActivity(new Intent("com.aleksei.etb.END"));
return;
}
button_click = MediaPlayer.create(this, R.raw.button_click);
button_click.start();
switch(view.getId()){
case R.id.button5: //main
break;
case R.id.button1: //answer_1
if(isCorrect(1))
correct++;
break;
case R.id.button2: //answer_2
if(isCorrect(2))
correct++;
break;
case R.id.button3: //answer_3
if(isCorrect(3))
correct++;
break;
case R.id.button4: //answer_3
if(isCorrect(4))
correct++;
break;
default:
break;
}
Game(i1);
tip.setText("Correct answers: "+correct);
}
public static int getResults(){
int value = (int) Math.floor((correct*5)/question_amount);
if(value <= 0)
return 1;
else
return value;
}
private boolean isCorrect(int button){
for (int i = 0; i < answers_correct.length; i++){
if(button == 1 && a1 == answers_correct[i]
|| button == 2 && a2 == answers_correct[i]
|| button == 3 && a3 == answers_correct[i]
|| button == 4 && a4 == answers_correct[i])
return true;
}
return false;
}
private void Game(int q){
if(i1 == question_amount) { //no more questions
startActivity(new Intent("com.aleksei.etb.END"));
alive = false;
return;
}
try {
main.setText("Dunno");
String answer_list[] = {
answers_correct[q], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]
};
Collections.shuffle(Arrays.asList(answer_list));
answer_1.setText(answer_list[0]);
answer_2.setText(answer_list[1]);
answer_3.setText(answer_list[2]);
answer_4.setText(answer_list[3]);
a1 = answer_list[0];
a2 = answer_list[1];
a3 = answer_list[2];
a4 = answer_list[3];
q_textview.setText(questions[q]);
} catch (Exception ex){}
i1++;
}
private void getData(){
//Getting the data
main = (Button) findViewById(R.id.button5);
answer_1 = (Button) findViewById(R.id.button1);
answer_2 = (Button) findViewById(R.id.button2);
answer_3 = (Button) findViewById(R.id.button3);
answer_4 = (Button) findViewById(R.id.button4);
q_textview = (TextView) findViewById(R.id.question);
tip = (TextView) findViewById(R.id.answ1);
//Making the buttons, actually work
main.setOnClickListener(this);
answer_1.setOnClickListener(this);
answer_2.setOnClickListener(this);
answer_3.setOnClickListener(this);
answer_4.setOnClickListener(this);
//Resets the text
//Note to self: Replace with another ContectView
main.setText("Begin!");
answer_4.setText("");
answer_3.setText("");
answer_2.setText("");
answer_1.setText("");
tip.setText("");
}
}
Consider the following changes and see if it improves:
private int questionNumber = -1;
private void Game(int q){
...
questionNumber = q;
q_textview.setText(questions[q]);
...
}
private boolean isCorrect(int button){
if(button == 1 && a1.equals(answers_correct[questionNumber])
|| button == 2 && a2.equals(answers_correct[questionNumber])
|| button == 3 && a3.equals(answers_correct[questionNumber])
|| button == 4 && a4.equals(answers_correct[questionNumber]))
return true;
}
return false;
}
Storing the question number as a field eliminates the for loop over all the questions.
You should always use String.equals instead of ==.