根据Spinner 1中的选择动态更新Spinner2 [英] Dynamic update of Spinner2 based on choice in Spinner 1

查看:206
本文介绍了根据Spinner 1中的选择动态更新Spinner2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用此代码(改编自这里)我得到以下错误:

In using this code (adapted from here) I get the following error:


构造函数ArrayAdapter新
AdapterView.OnItemSelectedListener(){},
int,String [])未定义

The constructor ArrayAdapter(new AdapterView.OnItemSelectedListener(){}, int, String[]) is undefined

任何人帮助纠正这个感谢

Can anyone help correct this? Thanks

private void setDynamicSpinnerBE() {
    beSpinner = (Spinner) findViewById(R.id.spBE);
    final String[] itemsSpinner2;
    lensSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            Integer focalLength = (Integer) parent.getAdapter().getItem(
                    position);
            itemsSpinner2 = getBellowsExtension(focalLength, maxBellows,
                    precision);

            ArrayAdapter<CharSequence> beAdapter = new ArrayAdapter<CharSequence>(
                    this, android.R.layout.simple_spinner_item,
                    itemsSpinner2);
            beAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            beSpinner.setAdapter(beAdapter);

        }

        public void onNothingSelected(AdapterView<?> parent) {
            beSpinner.setAdapter(null);
        }
    }

    );
    ...
}


推荐答案

在下面这行的新的 ArrayAdapter 的构造函数中:

In the constructor to your new ArrayAdapter on the following line:

ArrayAdapter<CharSequence> beAdapter = new ArrayAdapter<CharSequence>(this,

指向您所在的 OnItemSelectedListener 类的当前实例,而不是父视图,这是由于 ArrayAdapter 没有匹配的构造函数,你应该尝试使用 MyParentView.this (其中 MyParentView

this points to the current instance of the OnItemSelectedListener class you are in and not the parent view, this is where the problem occurs because ArrayAdapter does not have a matching constructor. You should try using MyParentView.this (where MyParentView is the name of the view you are in) instead to pass the appropriate instance.

这篇关于根据Spinner 1中的选择动态更新Spinner2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆