简体   繁体   English

EditText不断返回Empty-Android

[英]EditText constantly returns Empty - Android

I'm attempting to null check an EditText (which should be simple enough) however each time I enter text into my two ET's - they always return as empty: "" and they should not (if they have text in them). 我试图对一个EditText进行空检查(这应该很简单),但是每次我在两个ET中输入文本时-它们总是返回为空:“”,并且它们不应该(如果其中包含文本)。

I've looked over several other SO articles related to this and none of the fixes seem to resolve the issue. 我查看了与此相关的其他几篇SO文章,但这些修复似乎都无法解决该问题。

Can anyone spot what I might be doing wrong in this instance? 任何人都可以发现我在这种情况下可能做错了什么吗?

Source Snippet: 源代码段:

Java class: Java类:

... ...

public void doneClicked(View v) throws JSONException {

    LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v1 = li.inflate(R.layout.profile_notification_settings_list_edit, null);

    final EditText label = (EditText) v1.findViewById(R.id.emaillabel);
    final EditText value = (EditText) v1.findViewById(R.id.emailvalue);

    String sValue = value.getText().toString();
    if (sValue.matches("")) {
        Toast.makeText(this, "You did not enter a value", Toast.LENGTH_SHORT).show();
        return;
    }

    String sLabel = label.getText().toString();
    if (sLabel.matches("")) {
        Toast.makeText(this, "You did not enter a label", Toast.LENGTH_SHORT).show();
        return;
    }

... ...

Adapter: 适配器:

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    view = inflater.inflate(R.layout.profile_notification_settings_list_edit, null);
    EditText value = (EditText) view.findViewById(R.id.emailvalue);
    value.setHint("Value");
    value.setText(valueList[i]);

    EditText label = (EditText) view.findViewById(R.id.emaillabel);
    if (labelList != null)
        label.setHint("Label");
    label.setText(labelList[i]);

Full source: 完整资料:

Java Class: Java类:

https://pastebin.com/y1e9J1yE https://pastebin.com/y1e9J1yE

Adapter: 适配器:

https://pastebin.com/Zqh3eCAk https://pastebin.com/Zqh3eCAk

Because everytime you click you are creating a new view which has nothing to do with the views currently being displayed on the screen 因为每次单击都在创建一个新view ,该视图与当前在屏幕上显示的视图无关

public void doneClicked(View v) throws JSONException {

    // don't inflate new views
    LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v1 = li.inflate(R.layout.profile_notification_settings_list_edit, null);

    final EditText label = (EditText) v1.findViewById(R.id.emaillabel);
    final EditText value = (EditText) v1.findViewById(R.id.emailvalue);

    // ... code

Note : you cannot directly fetch the data from rowItemVies from ListView , so you need to add method in your adapter class to get the selected position through which you can fetch the rowView using yourListView.getChildAt(position) and further fetch data from children in rowView 注意 :您不能直接从ListView rowItemVies获取数据,因此您需要在适配器类中添加方法以获取选定的位置,您可以通过该位置使用yourListView.getChildAt(position)来获取rowView并进一步从rowView子项中获取数据

使用sValue.trim()。equals(“”)而不是sValue.matches(“”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM