英文:
How to make Material Exposed Drop Down List required field in Java
问题
我正在使用Material Exposed Drop Down List将信息添加到我的Firebase数据库,我需要在保存信息之前将下拉列表设置为必填字段。目前,信息会保存,但保存的字段为空,这会导致我的应用在其他活动中崩溃。
我目前正在使用的代码将下拉列表视为EditText,但这不起作用。
我正在定义下拉菜单并捕获用户输入(如果有的话)如下:
// CPD TYPE DROPDOWN MENU
cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
cpdType = findViewById(R.id.cpdType);
final String[] type = new String[]{
"Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
};
final ArrayAdapter<String> adapterType = new ArrayAdapter<>(
AddActivity.this,
R.layout.dropdown_item,
type
);
cpdType.setAdapter(adapterType);
// TO CAPTURE USER SELECTION FROM DROP DOWN LIST
((AutoCompleteTextView) cpdTypeLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapterType.getItem(position);
selectedType = ((AutoCompleteTextView) cpdTypeLayout.getEditText()).getText().toString();
Log.d("TAG", "selected type is: " + selectedType);
}
});
然后,我使用以下代码检查是否有任何字段为空,然而,这些检查无法阻止保存操作:
if (selectedType.isEmpty() || selectedHours.isEmpty() || selectedMins.isEmpty()) {
Toast.makeText(AddActivity.this, "无法保存空字段的活动", Toast.LENGTH_SHORT).show();
return;
}
我还尝试过使用 selectedType == null
,但这也不起作用。
我不确定是否需要在XML文件或Java文件中添加一些内容来防止这种情况发生。
英文:
I am using Material Exposed Drop Down List to add information to my Firebase database, and I need to make the drop down lists a required field before the information saves. Currently the information will save but saves the field as null, which makes my add crash on a different activity.
The code I am currently using at the moment treats the drop down like an EditText, but this isn't working.
I am defining the drop down menu and capturing the user input (if made) like this:
//CPD TYPE DROPDOWN MENU
cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
cpdType = findViewById(R.id.cpdType);
final String[] type = new String[]{
"Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
};
final ArrayAdapter<String> adapterType = new ArrayAdapter<>(
AddActivity.this,
R.layout.dropdown_item,
type
);
cpdType.setAdapter(adapterType);
//TO CAPTURE USER SELECTION FROM DROP DOWN LIST
((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapterType.getItem(position);
selectedType = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
Log.d("TAG", "selected type is: " + selectedType);
}
});
I am then using this code to check if anything is empty before adding to Firebase, but the checks do not stop the save being made.
if (selectedType.isEmpty() || selectedHours.isEmpty() || selectedMins.isEmpty()) {
Toast.makeText(AddActivity.this, "Can not save activity with empty fields", Toast.LENGTH_SHORT).show();
return;
}
I have also tried using selectedType == null
but this doesn't work either.
I'm not sure if I need to add something to my XML file or in my Java file to prevent this from happening
答案1
得分: 0
我成功地通过在我的保存按钮点击事件中添加以下内容来解决了这个问题:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedType.trim().length() != 0 && selectedHours.trim().length() != 0 && selectedMins.trim().length() != 0){
uploadToFirebase();
} else {
if (selectedType.trim().length() == 0) {
cpdTypeLayout.setError("请选择活动类型");
}
if (selectedHours.trim().length() == 0) {
cpdHoursLayout.setError("请选择小时数");
}
if (selectedMins.trim().length() == 0){
cpdMinsLayout.setError("请选择分钟数");
}
}
}
});
英文:
I managed to solve this by myself by adding the following to my save button click:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (selectedType.trim().length() != 0 && selectedHours.trim().length() != 0 && selectedMins.trim().length() != 0){
uploadToFirebase();
} else {
if (selectedType.trim().length() == 0) {
cpdTypeLayout.setError("Please select Activity Type");
}
if (selectedHours.trim().length() == 0) {
cpdHoursLayout.setError("Please select Hours");
}
if (selectedMins.trim().length() == 0){
cpdMinsLayout.setError("Please select Minutes");
}
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论