I'm comparing the input from a TextEdit with a answer from the "answerList". Now I'm wondering: Why does the .equals() not compare the "uinput" String? Could someone explain this to me and put it in use in the code?
Thanks in advance and have a good day!
package ...
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView view1;
public String uinput;
public EditText edit1;
public TextView score_view;
public int score = 0;
public String[] questionList = {
"lux, luces",
"munus, munera",
"neglere",
};
public String[] answerList = {
"(dag)licht, dag",
"taak",
"verwaarlozen",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.edit1 = findViewById(R.id.edit1);
this.view1 = findViewById(R.id.view1);
this.score_view = findViewById(R.id.score_view);
this.uinput = edit1.getText().toString();
view1.setText(questionList[0]);
}
public void check(View view) {
if (uinput.equals(answerList[0])) {
edit1.setBackgroundColor(Color.parseColor("#00FF00"));
score++;
score_view.setText(score);
} else {
edit1.setBackgroundColor(Color.parseColor("#FF0000"));
}
}
}
checkmethod theuinput.equals(answerList[0])might not succeed? If so, it is likely because unless you set theuinputto the updated value ofedit1.getText()elsewhere, it only has the value when it was first created, which would be an empty String. Try printing the the value ofuinputbefore the test in thecheckmethod. If I have not understood the issue, please elaborate the specific issue you are seeing by providing current results and expected results.