【问题标题】:Switch within a loop cannot execute twice循环内的切换不能执行两次
【发布时间】:2016-09-18 18:38:38
【问题描述】:

我将通过在用户修改和更新之前将文本设置为先前输入的editTexts,将文本设置为配置文件数据字段。

以下是代码:

原始数据是一个字符串: {k1=v1, k2=v2, k3=v3, .....k9=v9}

public void parse(String foo) {
        String foo2 = foo.substring(1, foo.length() - 1);  // hack off braces

        StringTokenizer st = new StringTokenizer(foo2, ",");
        String[] key = new String[20];
        String[] value = new String[20];
        int i = 0;
        while (st.hasMoreTokens()) {
            String thisToken = st.nextToken();        
            String[] keyValue = thisToken.split("=");
            try {
                key[i] = keyValue[0];
                value[i] = keyValue[1];
                setTextToFields(key[i], value[i]);
                i++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }          
        //for (i = 0; key[i] != null && value[i] != null && i <= 9; i++) {            
        //    setTextToFields(key[i], value[i]);

    //}

我调用了 setTextToFields 方法来设置文本,如下:

public void setTextToFields(String key, String value) {
        String dateOfBirth = null;

        switch (key) {
            case "dateOfBirth":
                dateOfBirth = value;
                try {
                    etYear.setText(dateOfBirth.substring(0, 4), TextView.BufferType.EDITABLE);
                    etMonth.setText(dateOfBirth.substring(5, 7), TextView.BufferType.EDITABLE);
                    etDay.setText(dateOfBirth.substring(8), TextView.BufferType.EDITABLE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            case "firstName":
                etFirstName.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "lastName":
                etLastName.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "country":
                etCountry.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "city":
                etCity.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "iDNumber":
                etIDNumber.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "postCode":
                etPostCode.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "mobilePhoneNumber":
                etMobilePhoneNumber.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "province":
                etProvince.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "address":
                etAddress.setText(value, TextView.BufferType.EDITABLE);
                return;
            default:
                Toast.makeText(ProfileActivity.this,"Nothing set to text", Toast.LENGTH_SHORT).show();
                return;
}

当我第二次或更多次调用此 setTextToFields 方法时,开关只选择默认值,而没有设置任何 editText 视图。

请帮助并告诉我原因。

【问题讨论】:

标签: android for-loop switch-statement


【解决方案1】:

您是否应该在 while 循环中增加 i

您只设置了 key0 和 value0。剩下的都是空的,所以 for 循环和 end 只会被输入一次。

此外,您可以更新正则表达式以捕获等号周围的所有空格

int i = 0;
while (st.hasMoreTokens()) {
    String thisToken = st.nextToken();        
    String[] keyValue = thisToken.split("\\s*=\\s*");
    try {
        key[i] = keyValue[0];
        value[i] = keyValue[1];

或者,只需将setTextToFields(key[i], value[i]); 放在while 循环中

我还建议使用 HashMap 而不是两个数组。

【讨论】:

  • 这是一个很好的收获:)
  • 比我原来的答案好多了;)
  • 感谢您的回答。键值确实以两种方式传递给 setTextToFields,但是,开关不起作用。监控 Toast 告诉我“没有设置文本”,甚至一次都没有。
  • 我的回答在技术上仍然是正确的,但因为您尚未设置所有关键值。您可以将key[i].trim() 作为方法的参数。
  • 谢谢,只知道如何接受您的提示。
【解决方案2】:

终于,我找到了解决问题的正确方法。

传递给方法的 key[i] 未被修剪。因此,只需在 key 后面添加 trim() 即可。

switch (key.trim()) {
            case "dateOfBirth":
                dateOfBirth = value;
                try {
                    etYear.setText(dateOfBirth.substring(0, 4), TextView.BufferType.EDITABLE);
                    etMonth.setText(dateOfBirth.substring(5, 7), TextView.BufferType.EDITABLE);
                    etDay.setText(dateOfBirth.substring(8), TextView.BufferType.EDITABLE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            case "firstName":
                etFirstName.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "lastName":
                etLastName.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "country":
                etCountry.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "city":
                etCity.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "iDNumber":
                etIDNumber.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "postCode":
                etPostCode.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "mobilePhoneNumber":
                etMobilePhoneNumber.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "province":
                etProvince.setText(value, TextView.BufferType.EDITABLE);
                return;
            case "address":
                etAddress.setText(value, TextView.BufferType.EDITABLE);
                return;
            default:
                Toast.makeText(ProfileActivity.this,"Nothing set to text", Toast.LENGTH_SHORT).show();
                return;
    }

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2012-04-27
    • 2013-04-05
    • 2019-05-28
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多