我正在制作一个 Android 应用程序,可以让您查看是否有可用的日期。如果日期不在数组列表中,则会添加该日期,如果日期在数组列表中,则用户将收到一条消息“请重试”。
无论输入的日期是否已在数组列表中,If 语句都不起作用,并且永远不会给出“请重试”消息。我已经确认数据正在写入数组列表。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
final ArrayList<String> DateArray = new ArrayList<String>();
DateArray.add("0101"); //test code
addListenerRadioGroup(); //not important
mNextButton = (Button)findViewById(R.id.NextButton);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText2 =(EditText)findViewById(R.id.editText2);
if (DateArray.contains(mEditText2)) { //looks for edit text info
Toast.makeText(getApplicationContext(), //This should pop up if user
"please try again", Toast.LENGTH_SHORT).show(); //enters "0101"
} else {
DateArray.add(mEditText2.getText().toString()); }
}
});
}
最佳答案
看起来您的 if 语句条件正在测试 EditText 对象是否在 ArrayList 中。您可能想要测试 EditText 对象的字符串值,因为您的 ArrayList 存储字符串。
关于java - 如果语句不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919843/