English 中文(简体)
Populate Spinner, whichmapstring to number from XML in Anders
原标题:Populate Spinner that maps strings to numbers from XML in Android

在我的申请中,我需要具备一套价值观,例如:

  • 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" />

你们对此有何想法? 这是否比第一个目标好? 是否有其他办法执行这些事情?

问题回答

我建议你采用第二种方法。 当地化是明智的,你也重新取得预期的结果。





相关问题
how to use DynamicResource in the code behind?

I d like to be able to set a property to a dynamic resource programmatically. myControl.Property = this.Resource[key] is not a valid response, since if the resource with the key key is replaced, ...

PHP - Foreach loops and ressources

I m using a foreach loop to process a large set of items, unfortunately it s using alot of memory. (probably because It s doing a copy of the array). Apparently there is a way to save some memory with ...

C# Play MP3 from Resources with MCI or WMP control?

I have a MP3 file in my Resources of Visual C#. I m trying to find out if there is a way I can play this MP3 in a Windows Media Player control or with MCI, I m not particular. I m fairly new to C#. ...

Convert files of any types to a file with c strings

Please suggest a small command-line utility (for Windows) to convert files from particular directory to a valid c file. Maybe it can be done just with batch commands? The resulting file should look ...

How to sort ResourceSet in C#

I have a resource file named filetypes.resx. Some how I figured out to bind the resource values to dropdownlist, but I don t know how to sort the values of ResourceSet. Here is what I did so far, ...

热门标签