0

我已经预定义了类别名称以及客户给我的相应机器代码。例如:

BLDG-EXT = 外部建筑问题
BLDG-INT = 内部建筑问题
CUSTOD = 保管服务

这些只是大约二十个中的三个示例。问题是,我希望我的 Spinner 向用户显示人类可读的选项(例如“外部建筑问题”),但是当数据保存并存储到我的 SQLite 数据库中时,我希望将其保存为机器代码。

我怎样才能做到这一点?

谢谢你,
亚历克斯

编辑:这是我当前的实现,它不包括机器可读的类别名称,只包括字符串。

生成 Spinner(在 onCreate() 方法内):

Spinner spinner = (Spinner) findViewById(R.id.issue_category_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.issue_categories,
        android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if (null != issue.get_category()) {
    spinner.setSelection(adapter.getPosition(issue.get_category()));
}

按下按钮保存数据:

private void save_issue() {
    // Save field data to current issue
    issue.set_category(((Spinner) findViewById(R.id.issue_category_spinner))
            .getSelectedItem().toString());
    issue.set_building_code(((EditText) findViewById(R.id.issue_field_building_code))
            .getText().toString().toUpperCase());
    issue.set_floor_code(((EditText) findViewById(R.id.issue_field_floor_code))
            .getText().toString().toUpperCase());
    issue.set_location(((EditText) findViewById(R.id.issue_field_location))
            .getText().toString());
    issue.set_priority((int) ((PriorityRatingBar) findViewById(R.id.issue_field_priority))
            .getRating());
    issue.set_description(((EditText) findViewById(R.id.issue_field_description))
            .getText().toString());
    // Then save the current issue to the DB
    if (update) {
        db.updateIssue(issue);
    } else {
        issue.set_audit_id(getIntent().getExtras().getLong(
                "me.alexgleason.project.Audit"));
        issue = db.addIssue(issue);
    }
    // Save image
    File image = new File(get_storage_dir(), "temp.jpg");
    image.renameTo(new File(get_storage_dir(), issue.get_uuid() + ".jpg"));
    // Toasty
    Toast.makeText(getApplicationContext(), "Issue Saved",
            Toast.LENGTH_LONG).show();
    Intent intent = new Intent();
    setResult(RESULT_OK, intent);
    finish();
}

在strings.xml中

<string-array name="issue_categories">
    <item>Other</item>
    <item>Paint</item>
    <item>Lighting</item>
    <item>Flooring</item>
    <item>Custodial</item>
    <item>Furniture</item>
    <item>Exterior</item>
    <item>Plumbing</item>
    <item>Signage</item>
    <item>Parking</item>
    <item>Landscaping</item>
</string-array>
4

1 回答 1

0

我会推荐两个不同的数组,每个等效项在每个数组中的相同位置,所以你最终会做这样的事情;

   String[] machineCodes = getResources().getStringArray(R.array.machineCodes);
      String[] manCodes = getResources().getStringArray(R.array.manCodes);

  //set manCodes to spinner and spinner style

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> a, View v, int which, long id) {
                                    String machineCode = machineCodes[which];
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                            }
        });
于 2013-08-21T20:37:28.687 回答