如何在微调器中获取所选项的值?

时间:2015-02-19 17:43:53

标签: java android xml spinner android-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;

公共类MainActivity扩展ActionBarActivity实现AdapterView.OnItemSelectedListener {     public Spinner 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 ... 

}

3 个答案:

答案 0 :(得分:1)

  

我收到此错误:无法解析符号setOnItemSelectedListener

OnItemSelectedListener中实施MainActivity界面:

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

并在为当前活动设置布局后调用findViewById,因为在设置活动其他应用程序的布局后xml中的视图将因NPE

而崩溃

答案 1 :(得分:1)

您可以使用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)
    }
}

答案 2 :(得分:1)

首先,您需要实现要使用方法的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);  
....