Android 无法应用AdapterView中的setOnItemSelectedListener

Android 无法应用AdapterView中的setOnItemSelectedListener,android,spinner,Android,Spinner,我有一个活动,上面有两个旋转器 在onCreate中,我声明微调器: Spinner spinPhotographer = (Spinner) findViewById(R.id.spinPhotographer); Spinner spinLocation = (Spinner) findViewById(R.id.spinLocation); 然后,我调用一个void子函数,并将要用于填充微调器的文本文件的文件名传递给该方法。每个微调器都有自己的文本文件 populateSpinner(

我有一个活动,上面有两个旋转器

在onCreate中,我声明微调器:

Spinner spinPhotographer = (Spinner) findViewById(R.id.spinPhotographer);

Spinner spinLocation = (Spinner) findViewById(R.id.spinLocation);
然后,我调用一个void子函数,并将要用于填充微调器的文本文件的文件名传递给该方法。每个微调器都有自己的文本文件

populateSpinner("Photographers");

populateSpinner("Locations");
以下是读取文本文件并填充相应微调器的方法:

private void populateSpinner(String spinnerName) {
    String fileName = ("/sdcard/guestlink/" + spinnerName + ".txt");
    String line;
    ArrayList<String> names = new ArrayList<String>();

    //Read the lines of text into an ArrayList
    try {
        BufferedReader input = new BufferedReader(new FileReader(fileName));

    if (!input.ready()) {
            throw new IOException();
        }
        while ((line = input.readLine()) != null) {
            names.add(line);
        }
        input.close();
        //Sort the list alphabetically
        Collections.sort(names);
        //Build the ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter(this, 
android.R.layout.simple_spinner_dropdown_item, names);

        switch (spinnerName) {
            case "Photographers": {
                //Get handle for spinner object
                Spinner spinPhotographers = (Spinner) 
findViewById(R.id.spinPhotographer);
                //Populate the spinner from the array
                spinPhotographers.setAdapter(adapter);
                spinPhotographers.setOnItemSelectedListener(this);
            }
            case "Locations":{
                //Get handle for spinner object
                Spinner spinLocations = (Spinner) 
findViewById(R.id.spinLocation);
                //Populate the spinner from the array
                spinLocations.setAdapter(adapter);
                spinLocations.setBackgroundColor(Color.WHITE);
                spinLocations.setOnItemSelectedListener(this);
            }
        }
    } catch (IOException ex) {
        System.out.println(ex);
        Utilities.writeToLog(ex.toString(), logFile);
    }


}
private void populateSpinner(字符串喷丝头名称){
字符串文件名=(“/sdcard/guestlink/”+spinnerName+“.txt”);
弦线;
ArrayList name=新的ArrayList();
//将文本行读入ArrayList
试一试{
BufferedReader输入=新BufferedReader(新文件读取器(文件名));
如果(!input.ready()){
抛出新IOException();
}
而((line=input.readLine())!=null){
名称。添加(行);
}
input.close();
//按字母顺序对列表排序
集合。排序(名称);
//构建ArrayAdapter
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_微调器_下拉列表_项目,名称);
开关(喷丝头名称){
案例“摄影师”:{
//获取微调器对象的句柄
旋转器旋转摄影师=(旋转器)
FindViewbyd(R.id.Spin摄影师);
//从阵列填充微调器
旋转摄影师.设置适配器(适配器);
旋转摄影师。SetonimSelectedListener(此);
}
案例“地点”:{
//获取微调器对象的句柄
微调器微调位置=(微调器)
findViewById(R.id.spinLocation);
//从阵列填充微调器
设置适配器(适配器);
spinLocations.setBackgroundColor(颜色:白色);
spinLocations.setOnItemSelectedListener(此);
}
}
}捕获(IOEX异常){
系统输出打印项次(ex);
writeToLog(例如toString(),logFile);
}
}
在spinxxx.setOnItemSelectedListener行中(this);我得到以下错误:“无法应用AdapterView中的setOnItemSelectedListener”我已尝试将.setOnItem。。。在onCreate区域中。我就是不明白。微调器都从文本文件中正确填充

我还使用此方法对所选的选项作出反应。我的计划是使用arg0找出哪个微调器选择了一个项目

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long 
arg3) {
    switch(arg0.getId()){
        case R.id.spinPhotographer:
            break;
        case R.id.spinLocation;
            break;
        case R.id.spinGuestidLength:
            break;
    }
}
public void已选中(AdapterView arg0、View arg1、int arg2、long
arg3){
开关(arg0.getId()){
案例R.id.1:
打破
病例R.id.位置;
打破
案例R.id.spinguide长度:
打破
}
}

您的
setOnItemSelectedListener
接收
,指的是您当前的活动。您可能没有实现接口
AdapterView.OnItemClickListener。

如果您不希望实现该接口,您也可以从
onCreate
尝试此操作,而不是执行
旋转摄影师。setOnItemSelectedListener(此)

SpinPictures.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View、,
内部位置,长id){
对象项=adapterView.getItemAtPosition(位置);
//...
}
}

您的
setOnItemSelectedListener
接受
,指的是您当前的活动。您可能没有实现接口
AdapterView.OnItemClickListener。

如果您不希望实现该接口,您也可以从
onCreate
尝试此操作,而不是执行
旋转摄影师。setOnItemSelectedListener(此)

SpinPictures.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View、,
内部位置,长id){
对象项=adapterView.getItemAtPosition(位置);
//...
}
}

您在其中声明上述所有内容的活动需要实现
AdapterView.OnItemSelectedListener
,或者在逻辑上实现
setOnItemSelectedListener(此)
将失败。
引用活动。

您在其中声明上述所有内容的活动需要实现
AdapterView.OnItemSelectedListener
,或者从逻辑上说,
setOnItemSelectedListener(此)
将失败。
指的是活动。

这是一个非常简单的修复;您只需确保您的
实现。

这是一个非常简单的修复;您只需确保您的
实现。

在您选择的
监听器中添加此选项,而不是您的

Spinner spinner = (Spinner) parent;
    switch (spinner.getId()) {

case R.id.spinPhotographer:
        break;
case R.id.spinLocation;
        break;
case R.id.spinGuestidLength:
        break;



}

在您的
onimselected
监听器中添加此选项,而不是您的

Spinner spinner = (Spinner) parent;
    switch (spinner.getId()) {

case R.id.spinPhotographer:
        break;
case R.id.spinLocation;
        break;
case R.id.spinGuestidLength:
        break;



}
实施你的活动

implements AdapterView.OnItemSelectedListener
在onCreate内部添加此行

 spinPhotographer .setOnItemSelectedListener(this);
 spinLocation .setOnItemSelectedListener(this);
实施你的活动

implements AdapterView.OnItemSelectedListener
在onCreate内部添加此行

 spinPhotographer .setOnItemSelectedListener(this);
 spinLocation .setOnItemSelectedListener(this);

不必要的强制转换是一种可怕的习惯…如果
这将意味着
活动
这将在给定的接口中实现,那么强制转换是不好的…fx您将删除实现,并且您的代码仍将编译,但您将得到运行时异常…不,我没有否决这个问题,因为第一部分应该解决问题ssary强制转换是一个可怕的习惯…如果
这个
将意味着
活动
,这将在给定的接口中实现,那么强制转换是不好的…fx您将删除实现,您的代码仍将编译,但您将得到运行时异常…不,我没有否决这个问题,因为第一部分应该解决我必须解决的问题我不知道抽象对我来说有什么作用,但它是