根据性别前缀应反映

时间:2018-05-08 08:30:08

标签: android spinner

在我的活动中,有两个spinners -perfix spinner和性别spinner。 我想做 - 当我选择性别时 - 女性它应该反映前缀微调中女性的前缀。我该怎么办?

性别的微调:

spnrGenderList=new ArrayList<>();
spnrGenderList.add("Male");
spnrGenderList.add("Female");

Spinner for Prefix

spnrPrefixList=new ArrayList<>();
spnrPrefixList.add(getResources().getString(R.string.mr));
spnrPrefixList.add(getResources().getString(R.string.mrs));
spnrPrefixList.add(getResources().getString(R.string.ms));
spnrPrefixList.add(getResources().getString(R.string.miss));

1 个答案:

答案 0 :(得分:0)

您可以使用setOnItemSelectedListener视图中的Spinner来实现这一目标。

    final List<String> spnrGenderList = new ArrayList<>();
    spnrGenderList.add("Male");
    spnrGenderList.add("Female");

    final List<String> spnrPrefixList = new ArrayList<>();

    genderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selectedItem = parent.getItemAtPosition(position).toString();
            spnrPrefixList.clear();
            if (selectedItem.equals(spnrGenderList.get(0))) {
                spnrPrefixList.add(getResources().getString(R.string.mr));
            } else {
                spnrPrefixList.add(getResources().getString(R.string.mrs));
                spnrPrefixList.add(getResources().getString(R.string.ms));
                spnrPrefixList.add(getResources().getString(R.string.miss));
            }
            prefixSpinnerAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });