【发布时间】: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 视图。
请帮助并告诉我原因。
【问题讨论】:
-
我会说不,它没有。可以发minimal reproducible example吗?
-
对不起,不明白你的意思。
标签: android for-loop switch-statement