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 ...
}
You can use getSelectedItem() method:
or using getItemAtPosition() method:
Implement
OnItemSelectedListener
interface inMainActivity
:Add
setOnItemSelectedListener
to Spinner insideonCreate
method :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 withNPE
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:Then, put
@Override
upon the methods to describe the behaviors you want:}
}
And then set your spinner listener inside your
onCreate()
: