Spinner2 基于 Spinner 1 中的选择进行动态更新

发布于 2024-10-08 04:26:09 字数 1391 浏览 1 评论 0原文

使用此代码(改编自 此处)我收到以下错误:

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

任何人都可以帮助纠正这个问题吗?谢谢

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);
        }
    }

    );
    ...
}

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

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);
        }
    }

    );
    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-10-15 04:26:09

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

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

this 指向您所在的 OnItemSelectedListener 类的当前实例,而不是父类查看,这就是问题发生的地方,因为 ArrayAdapter 没有匹配的构造函数。您应该尝试使用 MyParentView.this (其中 MyParentView 是您所在视图的名称)来传递适当的实例。

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

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

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.

落墨 2024-10-15 04:26:09

是的,这里是:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DynamicSpinner extends Activity {
    private Spinner firstSpinner, secondSpinner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        setAdapters();
        setListeners();
    }

private void findViews() {
    firstSpinner = (Spinner) findViewById(R.id.spLenses);
    secondSpinner = (Spinner) findViewById(R.id.spBE);
}

private void setAdapters() {
    ArrayAdapter<CharSequence> firstAdapter = ArrayAdapter.createFromResource(this, 
            R.array.lens_array, android.R.layout.simple_spinner_item);
    firstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    firstSpinner.setAdapter(firstAdapter);
}

/* 
 * This is the important part. The user's choice in Spinner1 is passed to 
 * the procedure getDataForSecondSpinner () which creates an array shown in Spinner2  
 */
private void setListeners() {
    firstSpinner.setOnItemSelectedListener( new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String userChoiceSpinner1 =  (String) parent.getSelectedItem(); 
            String[] itemsSpinner2 = getDataForSecondSpinner (Integer.parseInt(userChoiceSpinner1)); 

            ArrayAdapter<CharSequence> secondAdapter = new ArrayAdapter<CharSequence>(DynamicSpinner.this, 
                    android.R.layout.simple_spinner_item, itemsSpinner2);
            secondAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);       
            secondSpinner.setAdapter(secondAdapter);                
        }
        public void onNothingSelected(AdapterView<?> parent) {
            secondSpinner.setAdapter(null);
        }
      }
    );

    secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        }
        public void onNothingSelected (AdapterView<?> parent) {
        }
      }
    );
}

/*
 * Returns an array based on user choice in Spinner 1 
 */
private String[] getDataForSecondSpinner (int userChoiceSpinnerOne) { 
    //some arbitrary parameters needed for my example, ignore...
    int param2 = 330;
    double param3 = 0.17;
    int array_index = 0;

    int inputUserChoice = userChoiceSpinnerOne;

    float param5 =  (float) ( (float) param2 / (float)userChoiceSpinnerOne  - 1); 
    int array_size = (int) (Math.round(param5  / param3) + 2);
    String[] arrayBE= new String[array_size];

    while (inputUserChoice <= param2) {
        inputUserChoice = (int) (userChoiceSpinnerOne * (param3 * array_index + 1));
        if (inputUserChoice <= param2) { 
            arrayBE[array_index] = String.valueOf(inputUserChoice);
        }
        else {
            arrayBE[array_index] = String.valueOf(param2);
        }
        array_index = array_index + 1;
        }

    return arrayBE;
}


}

Yeah, here it is:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DynamicSpinner extends Activity {
    private Spinner firstSpinner, secondSpinner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        setAdapters();
        setListeners();
    }

private void findViews() {
    firstSpinner = (Spinner) findViewById(R.id.spLenses);
    secondSpinner = (Spinner) findViewById(R.id.spBE);
}

private void setAdapters() {
    ArrayAdapter<CharSequence> firstAdapter = ArrayAdapter.createFromResource(this, 
            R.array.lens_array, android.R.layout.simple_spinner_item);
    firstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    firstSpinner.setAdapter(firstAdapter);
}

/* 
 * This is the important part. The user's choice in Spinner1 is passed to 
 * the procedure getDataForSecondSpinner () which creates an array shown in Spinner2  
 */
private void setListeners() {
    firstSpinner.setOnItemSelectedListener( new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String userChoiceSpinner1 =  (String) parent.getSelectedItem(); 
            String[] itemsSpinner2 = getDataForSecondSpinner (Integer.parseInt(userChoiceSpinner1)); 

            ArrayAdapter<CharSequence> secondAdapter = new ArrayAdapter<CharSequence>(DynamicSpinner.this, 
                    android.R.layout.simple_spinner_item, itemsSpinner2);
            secondAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);       
            secondSpinner.setAdapter(secondAdapter);                
        }
        public void onNothingSelected(AdapterView<?> parent) {
            secondSpinner.setAdapter(null);
        }
      }
    );

    secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        }
        public void onNothingSelected (AdapterView<?> parent) {
        }
      }
    );
}

/*
 * Returns an array based on user choice in Spinner 1 
 */
private String[] getDataForSecondSpinner (int userChoiceSpinnerOne) { 
    //some arbitrary parameters needed for my example, ignore...
    int param2 = 330;
    double param3 = 0.17;
    int array_index = 0;

    int inputUserChoice = userChoiceSpinnerOne;

    float param5 =  (float) ( (float) param2 / (float)userChoiceSpinnerOne  - 1); 
    int array_size = (int) (Math.round(param5  / param3) + 2);
    String[] arrayBE= new String[array_size];

    while (inputUserChoice <= param2) {
        inputUserChoice = (int) (userChoiceSpinnerOne * (param3 * array_index + 1));
        if (inputUserChoice <= param2) { 
            arrayBE[array_index] = String.valueOf(inputUserChoice);
        }
        else {
            arrayBE[array_index] = String.valueOf(param2);
        }
        array_index = array_index + 1;
        }

    return arrayBE;
}


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文