如何在Java中将Material Exposed Drop Down列表设置为必填字段

huangapple go评论127阅读模式
英文:

How to make Material Exposed Drop Down List required field in Java

问题

我正在使用Material Exposed Drop Down List将信息添加到我的Firebase数据库,我需要在保存信息之前将下拉列表设置为必填字段。目前,信息会保存,但保存的字段为空,这会导致我的应用在其他活动中崩溃。

我目前正在使用的代码将下拉列表视为EditText,但这不起作用。

我正在定义下拉菜单并捕获用户输入(如果有的话)如下:

  1. // CPD TYPE DROPDOWN MENU
  2. cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
  3. cpdType = findViewById(R.id.cpdType);
  4. final String[] type = new String[]{
  5. "Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
  6. };
  7. final ArrayAdapter<String> adapterType = new ArrayAdapter<>(
  8. AddActivity.this,
  9. R.layout.dropdown_item,
  10. type
  11. );
  12. cpdType.setAdapter(adapterType);
  13. // TO CAPTURE USER SELECTION FROM DROP DOWN LIST
  14. ((AutoCompleteTextView) cpdTypeLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
  15. @Override
  16. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  17. adapterType.getItem(position);
  18. selectedType = ((AutoCompleteTextView) cpdTypeLayout.getEditText()).getText().toString();
  19. Log.d("TAG", "selected type is: " + selectedType);
  20. }
  21. });

然后,我使用以下代码检查是否有任何字段为空,然而,这些检查无法阻止保存操作:

  1. if (selectedType.isEmpty() || selectedHours.isEmpty() || selectedMins.isEmpty()) {
  2. Toast.makeText(AddActivity.this, "无法保存空字段的活动", Toast.LENGTH_SHORT).show();
  3. return;
  4. }

我还尝试过使用 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:

  1. //CPD TYPE DROPDOWN MENU
  2. cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
  3. cpdType = findViewById(R.id.cpdType);
  4. final String[] type = new String[]{
  5. &quot;Formal Education Completed&quot;, &quot;Other Completed&quot;, &quot;Professional Activities&quot;, &quot;Self-Directed Learning&quot;, &quot;Work-Based Learning&quot;
  6. };
  7. final ArrayAdapter&lt;String&gt; adapterType = new ArrayAdapter&lt;&gt;(
  8. AddActivity.this,
  9. R.layout.dropdown_item,
  10. type
  11. );
  12. cpdType.setAdapter(adapterType);
  13. //TO CAPTURE USER SELECTION FROM DROP DOWN LIST
  14. ((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
  15. @Override
  16. public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  17. adapterType.getItem(position);
  18. selectedType = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
  19. Log.d(&quot;TAG&quot;, &quot;selected type is: &quot; + selectedType);
  20. }
  21. });

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.

  1. if (selectedType.isEmpty() || selectedHours.isEmpty() || selectedMins.isEmpty()) {
  2. Toast.makeText(AddActivity.this, &quot;Can not save activity with empty fields&quot;, Toast.LENGTH_SHORT).show();
  3. return;
  4. }

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

我成功地通过在我的保存按钮点击事件中添加以下内容来解决了这个问题:

  1. fab.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View view) {
  4. if (selectedType.trim().length() != 0 && selectedHours.trim().length() != 0 && selectedMins.trim().length() != 0){
  5. uploadToFirebase();
  6. } else {
  7. if (selectedType.trim().length() == 0) {
  8. cpdTypeLayout.setError("请选择活动类型");
  9. }
  10. if (selectedHours.trim().length() == 0) {
  11. cpdHoursLayout.setError("请选择小时数");
  12. }
  13. if (selectedMins.trim().length() == 0){
  14. cpdMinsLayout.setError("请选择分钟数");
  15. }
  16. }
  17. }
  18. });
英文:

I managed to solve this by myself by adding the following to my save button click:

  1. fab.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View view) {
  4. if (selectedType.trim().length() != 0 &amp;&amp; selectedHours.trim().length() != 0 &amp;&amp; selectedMins.trim().length() != 0){
  5. uploadToFirebase();
  6. } else {
  7. if (selectedType.trim().length() == 0) {
  8. cpdTypeLayout.setError(&quot;Please select Activity Type&quot;);
  9. }
  10. if (selectedHours.trim().length() == 0) {
  11. cpdHoursLayout.setError(&quot;Please select Hours&quot;);
  12. }
  13. if (selectedMins.trim().length() == 0){
  14. cpdMinsLayout.setError(&quot;Please select Minutes&quot;);
  15. }
  16. }
  17. }
  18. });

huangapple
  • 本文由 发表于 2020年7月30日 18:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63171120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定