Reputation: 13
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"));
}
}
}
Upvotes: 1
Views: 51
Reputation: 4403
The OP's question concerned a comparison of the uinput
to an element in the array questionList
. In the check
method, the comparison was performed against the uinput
, but the value of uinput
was not being updated prior to the check.
public void check(View view) {
// ADD HERE: update the value of the input
uinput = edit1.getText().toString();
if (uinput.equals(answerList[0])) {
edit1.setBackgroundColor(Color.parseColor("#00FF00"));
score++;
score_view.setText(score);
} else {
edit1.setBackgroundColor(Color.parseColor("#FF0000"));
}
}
Upvotes: 1
Reputation: 1710
remove this keyword
......
edit1 = findViewById(R.id.edit1);
view1 = findViewById(R.id.view1);
score_view = findViewById(R.id.score_view);
......
add this in OnClick() method
uinput = edit1.getText().toString();
Upvotes: 0