AdapterView中的setOnItemSelectedListener无法应用

时间:2017-06-26 12:14:27

标签: android spinner

我有一个带有2个Spinners的活动。

在onCreate中我声明了Spinners:

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

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

然后我调用一个void sub并将方法传递给我想用来填充Spinners的文本文件的文件名。每个Spinner都有自己的文本文件。

populateSpinner("Photographers");

populateSpinner("Locations");

以下是读取文本文件并填充相应Spinner的方法:

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


}

在spinxxx.setOnItemSelectedListener(this)行中;我收到以下错误:“AdapterView中的setOnItemSelectedListener无法应用”我尝试将.setOnItem ...放在onCreate区域。我只是不明白。 Spinners都可以从文本文件中正确填充。

我也让这个方法对onItemSelected做出反应。我的计划是使用arg0找出哪个Spinner选择了一个项目。

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

5 个答案:

答案 0 :(得分:1)

您声明上述所有需要实施AdapterView.OnItemSelectedListener或逻辑上setOnItemSelectedListener(this)的活动将失败。 this指的是活动。

答案 1 :(得分:1)

这很容易解决;您只需确保class实施AdapterView.OnItemClickListener

答案 2 :(得分:0)

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

如果您不希望实施该界面,也可以从onCreate尝试此操作,而不是spinPhotographers.setOnItemSelectedListener(this)

 spinPhotographers.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
                int position, long id) {
            Object item = adapterView.getItemAtPosition(position);
            //...

        }
 }

答案 3 :(得分:-1)

onItemSelected 听众中添加此内容而不是您的

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

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



}

答案 4 :(得分:-1)

实施你的活动

implements AdapterView.OnItemSelectedListener

在onCreate里面添加这一行

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