Android 循环中的开关不能执行两次
我将在用户修改和更新之前,通过将文本设置为先前输入的编辑文本,将文本设置为配置文件数据字段 以下是代码: 原始数据是一个字符串: {k1=v1,k2=v2,k3=v3,…k9=v9}Android 循环中的开关不能执行两次,android,for-loop,switch-statement,Android,For Loop,Switch Statement,我将在用户修改和更新之前,通过将文本设置为先前输入的编辑文本,将文本设置为配置文件数据字段 以下是代码: 原始数据是一个字符串: {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 StringTokenize
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方法时,开关仅选择默认值,而不将任何内容设置为editText视图
请帮忙并告诉我原因 是否应该在while循环中递增
i
您只设置了key0和value0。其余的为null,因此除了一次之外,不会输入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(键[i],值[i])代码>在while循环中
我还建议使用HashMap而不是两个数组 是否应该在while循环中递增i
您只设置了key0和value0。其余的为null,因此除了一次之外,不会输入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(键[i],值[i])代码>在while循环中
我还建议使用HashMap而不是两个数组 最后,我找到了解决问题的正确方法
传递给方法的键[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;
}
最后,我找到了解决问题的正确方法
传递给方法的键[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;
}
我会说不,它没有。你能发个帖子吗?对不起,我不明白你的意思。我会说不,它没有。你能发个帖子吗?对不起,我不明白你的意思。这是一个很好的发现:)比我原来的答案好多了;)谢谢你的回答。键值确实以两种方式传递给setTextToFields,但是,该开关不起作用。监控吐司告诉我“nothing set to text”,甚至一次也没有。我的回答在技术上仍然是正确的,尽管你没有设置所有的键值。你可以把键[i].trim()
作为方法的参数。谢谢,只需要知道如何接受你提示的答案。这是一个很好的发现:)比我原来的答案好得多;)谢谢你的回答。键值确实以两种方式传递给setTextToFields,但是,该开关不起作用。监控吐司告诉我“nothing set to text”,甚至一次也没有。我的回答在技术上仍然是正确的,尽管你没有设置所有的键值。您可以将键[i].trim()作为方法的参数。谢谢,只需知道如何接受提示中的答案。