在我的申请中,我需要具备一套价值观,例如:
- today
- tomorrow
- after_tomorrow
User chooses one of it and applications send request to server. To send the request i need to encode user s choice as int value:
- Request.TODAY = 0;
- Request.TOMORROW = 1;
- Request.AFTER_TOMORROW = 2;
在《倡议》中,这些价值是局部的,更重要的是,可能按不同顺序排列,取决于当地情况,例如:
- послезавтра (after_tomorrow)
- завтра (tomorrow)
- сегодня (today)
www.un.org/Depts/DGACM/index_spanish.htm 我想要的是,在可在当地使用的XML上,所有这一切都存在。 (因此,地方化小组只需要编辑XML档案,而不是编码):
- String values
- String order
我花了一段时间来思考这个问题,我的解决办法就是在这里。
In 座标:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="day_today">сегодня</string>
<string name="day_tomorrow">завтра</string>
<string name="day_after_tomorrow">послезавтра</string>
<array name="day_values">
<item>@string/day_after_tomorrow</item>
<item>@string/day_tomorrow</item>
<item>@string/day_today</item>
</array>
</resources>
在布局中:
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/day"
android:entries="@array/day_values" />
在我的活动中,java:
private int[] dayMap;
private Spinner uiDay;
private final void setupUI(Bundle savedInstanceState) {
uiDay = (Spinner)findViewById(R.id.day);
final TypedArray dayValues = getResources().obtainTypedArray(R.array.day_values);
dayMap = new int[dayValues.length()];
for (int i = 0; dayMap.length > i; ++i) {
final int r = dayValues.getResourceId(i, 0);
// can t use switch() since resources ids not always final since Android 4
if (R.string.day_today == r) {
dayMap[i] = Request.TODAY;
} else if (R.string.day_tomorrow == r) {
dayMap[i] = Request.TOMORROW;
} else if (R.string.after_tomorrow == r) {
dayMap[i] = Request.AFTER_TOMORROW;
}
}
dayValues.recycle();
}
public void submitClicked(View v) {
final int r = dayMap[uiDay.getSelectedItemPosition()];
submitDayRequest(r);
}
在该解决办法中,我只看到一件坏事——类型Array.getResourceId()返回“已解决的”id,这样,如果“如果”整个“不可行”将涉及某些转头。
我想听取关于这一解决办法的一些意见,并就如何以其他方式加以执行提出咨询意见。
感谢!
<><>Update1:
另一种解决办法是使用Xml文档中的两个阵列。
座标:
<string-array name="day_entries">
<item>послезавтра</item>
<item>завтра</item>
<item>сегодня</item>
</string-array>
<string-array name="day_values">
<item>after_tomorrow</item>
<item>tomorrow</item>
<item>today</item>
</string-array>
我的行动。 java:
private static final Map<String, Integer> DAYS;
static {
DAYS = new HashMap<String, Integer>();
DAYS.put("today", Request.TODAY);
DAYS.put("tomorrow", Request.TOMORROW);
DAYS.put("after_tomorrow", Request.AFTER_TOMORROW);
}
private Spinner uiDay;
private String[] dayValues;
private final void setupUI(Bundle savedInstanceState) {
uiDay = (Spinner)findViewById(R.id.day);
dayValues = getResources().getStringArray(R.array.day_values);
}
public void submitClicked(View v) {
final int r = DAYS.get(dayValues[uiDay.getSelectedItemPosition()]);
submitDayRequest(r);
}
排位:
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/day"
android:entries="@array/day_entries" />
你们对此有何想法? 这是否比第一个目标好? 是否有其他办法执行这些事情?