How to get the value of a selected item in a spinn

2019-09-07 07:19发布

I'm just learning Android app development, and I don't understand how to Log the value of a selected item from a spinner.

Here's what I have for 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);  
}

I'm following the Android dev docs on responding to user selections, but with my code, I get this error: Could not resolve symbol setOnItemSelectedListener

Why? And how do I fix it so I can Log what was selected in the Spinner?

EDIT

This code works

package com.example.compy.spookr;

public class MainActivity extends ActionBarActivity implements 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条回答
混吃等死
2楼-- · 2019-09-07 07:59

You can use getSelectedItem() method:

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

or using getItemAtPosition() method:

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)
    }
}
查看更多
疯言疯语
3楼-- · 2019-09-07 08:00

I get this error: Could not resolve symbol setOnItemSelectedListener

Implement OnItemSelectedListener interface in MainActivity :

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

Add setOnItemSelectedListener to Spinner inside onCreate method :

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

and also call findViewById after setting layout for current Activity because views from xml only available after setting layout for Activity other-wise application will crash with NPE

查看更多
何必那么认真
4楼-- · 2019-09-07 08:14

First of all, you need to implement the interface which you want to use the methods. In your case, you could do it like this:

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{

Then, put @Override upon the methods to describe the behaviors you want:

  @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

}

And then set your spinner listener inside your onCreate():

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactSpinner = (Spinner) findViewById(R.id.contact_list);
contactSpinner.setOnItemSelectedListener(this);  
....
查看更多
登录 后发表回答