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

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

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[]{
            &quot;Formal Education Completed&quot;, &quot;Other Completed&quot;, &quot;Professional Activities&quot;, &quot;Self-Directed Learning&quot;, &quot;Work-Based Learning&quot;
    };

    final ArrayAdapter&lt;String&gt; adapterType = new ArrayAdapter&lt;&gt;(
            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&lt;?&gt; parent, View view, int position, long id) {
            adapterType.getItem(position);
            selectedType = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
            Log.d(&quot;TAG&quot;, &quot;selected type is: &quot; + 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, &quot;Can not save activity with empty fields&quot;, 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 &amp;&amp; selectedHours.trim().length() != 0 &amp;&amp; selectedMins.trim().length() != 0){
                    uploadToFirebase();
                } else {
                    if (selectedType.trim().length() == 0) {
                        cpdTypeLayout.setError(&quot;Please select Activity Type&quot;);
                    }
                    if (selectedHours.trim().length() == 0) {
                        cpdHoursLayout.setError(&quot;Please select Hours&quot;);
                    }
                    if (selectedMins.trim().length() == 0){
                        cpdMinsLayout.setError(&quot;Please select Minutes&quot;);
                    }
                }
            }
        });

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:

确定