如何获取微调器中选定项目的值?

How to get the value of a selected item in a spinner?

我刚刚学习 Android 应用程序开发,我不明白如何 Log 从微调器中选择的项目的值。

这是我要 MainActivity.java

import android.content.ContentResolver;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.provider.ContactsContract;

import java.util.ArrayList;

    public class MainActivity extends ActionBarActivity {
        public Spinner contactSpinner = (Spinner) findViewById(R.id.contact_list);

    protected void onCreate(Bundle savedInstanceState) {
        ArrayList contactList = new ArrayList();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contactList.add("An item");  
        contactList.add("Another item");  
        contactList.add("A third item");  

        // Add items from contactList to spinner
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,contactList);
        contactSpinner.setAdapter(adapter);
    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        parent.getItemAtPosition(pos);

    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    contactSpinner.setOnItemSelectedListener(this);  
}

我在 responding to user selections 上关注 Android 开发文档,但是我的代码出现了这个错误:Could not resolve symbol setOnItemSelectedListener

为什么?以及如何修复它以便我可以 Log 在 Spinner 中选择了什么?

编辑

此代码有效

package com.example.compy.spookr;

public class MainActivity 扩展了 ActionBarActivity 实现 AdapterView.OnItemSelectedListener { public 微调器 contactSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList contactList = new ArrayList();
    contactSpinner = (Spinner) findViewById(R.id.contact_list);
    contactSpinner.setOnItemSelectedListener(this);
    // Rest of code...
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    parent.getItemAtPosition(pos);
    Log.v("onItemSelected",(String) parent.getItemAtPosition(pos));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

// Rest of code ... 

}

I get this error: Could not resolve symbol setOnItemSelectedListener

MainActivity中实现OnItemSelectedListener接口:

public class MainActivity extends ActionBarActivity 
                              implements OnItemSelectedListener{
  ....
}

setOnItemSelectedListener 添加到 onCreate 方法中的 Spinner :

public Spinner contactSpinner;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    contactSpinner = (Spinner) findViewById(R.id.contact_list);
    contactSpinner.setOnItemSelectedListener(this);  
     //... your code here..
}

并在为当前 Activity 设置布局后调用 findViewById 因为来自 xml 的视图仅在为 Activity 设置布局后才可用,否则应用程序将崩溃 NPE

您可以使用 getSelectedItem() 方法:

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String selected = spinner.getSelectedItem().toString();

或使用 getItemAtPosition() 方法:

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    ...
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }
}

首先,您需要实现您要使用的方法interface。在你的情况下,你可以这样做:

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

然后,在方法上加上 @Override 来描述您想要的行为:

  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);

}

   @Override
  public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback

}

然后在 onCreate():

中设置你的微调监听器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactSpinner = (Spinner) findViewById(R.id.contact_list);
contactSpinner.setOnItemSelectedListener(this);  
....