dimanche 25 octobre 2015

Compare the result of two TextViews Java/Android

I'm making a simple very simple android math game. But I cant manage to compare two TextViews to let the user know if they calculated correct or not???

Im not comparing them correctly as my if/else statement is only giving me the else output??

public class PlayActivity extends AppCompatActivity {

EditText number1;
EditText number2;
TextView result;
Button addNumbers;
TextView equalW;
TextView equalL;
TextView generate;


double num1,num2,sum;

Random r = new Random();

public View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        generate = (TextView)findViewById(R.id.textViewGenerate);

        int generated = r.nextInt(101);
        generate.setText(Integer.toString(generated));
    }
};

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

    number1 = (EditText)findViewById(R.id.editTextNumber1);
    number2 = (EditText)findViewById(R.id.editTextNumber2);
    result = (TextView)findViewById(R.id.textViewSum);
    addNumbers = (Button)findViewById(R.id.buttonAdd);
    equalL = (TextView)findViewById(R.id.textViewLose);
    equalW = (TextView)findViewById(R.id.textViewWin);
    Button buttonGenerate = (Button)findViewById(R.id.buttonGenerate);
    buttonGenerate.setOnClickListener(listener);


    addNumbers.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            num1 = Double.parseDouble(number1.getText().toString());
            num2 = Double.parseDouble(number2.getText().toString());

            sum = num1 + num2;
            result.setText(Double.toString(sum));

            if (generate.getText().toString().equals(result))
            {
                equalW.setText("Answer is correct");
            }
            else {
                equalL.setText("lose");
            }
        }
    });

}

Aucun commentaire:

Enregistrer un commentaire