在循环内切换不能执行两次

时间:2016-09-18 18:38:38

标签: android for-loop switch-statement

我将通过在用户修改和更新之前将文本设置为之前输入的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视图。

请帮忙,让我知道原因。

2 个答案:

答案 0 :(得分:2)

你应该在while循环中递增i吗?

您只设置了key0和value0。其余为空,因此除了一次之外,不会输入for循环和结尾。

此外,您可以更新正则表达式以捕获等于

周围的所有空格
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而不是两个数组。

答案 1 :(得分:0)

最后,我找到了解决问题的正确方法。

传递给方法的键[i]未被修剪。所以,只需在键后面添加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;
    }