xmlandroid-studio-import

Cannot Resolve symbol "OnItemSelectedListener"


package com.example.android.mycalculator;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity implements ***OnItemSelectedListener***{

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

        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        spinner.setOnItemSelectedListener(this);



        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("Multiply");
        categories.add("Divide");
        categories.add("Subtract");
        categories.add("Add");
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // On selecting a spinner item
        String item = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }
}

I am unable to import my OnItemSelectedListener Class on my main java file in android studio..I have selected the auto import settings..It is showing error for my above code saying;'cannot resolve 'OnItemSelectedListener'' when i use the interface for my class.


Solution

  • replace your code with the below code it will work now

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Spinner element
            Spinner spinner = (Spinner) findViewById(R.id.spinner);
    
            spinner.setOnItemSelectedListener(this);
    
    
    
            // Spinner Drop down elements
            List<String> categories = new ArrayList<String>();
            categories.add("Multiply");
            categories.add("Divide");
            categories.add("Subtract");
            categories.add("Add");
            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    
            // Drop down layout style - list view with radio button
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            // attaching data adapter to spinner
            spinner.setAdapter(dataAdapter);
            }
    
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // On selecting a spinner item
            String item = parent.getItemAtPosition(position).toString();
    
            // Showing selected spinner item
            Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
            }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    }