为spinner实现onItemSelectedListener

时间:2014-03-17 18:24:23

标签: java android eclipse

出现此错误:

"The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type 
 AdapterView<SpinnerAdapter> is not applicable for the arguments (SpinnerActivity)".

这些是我的.java文件。我是android和编程的新手。尝试创建一个toast并从微调器中显示用户选择。

package com.example.tipcalc;


import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class SpinnerActivity implements OnItemSelectedListener {

 public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), 
    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
    Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

}


package com.example.tipcalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    Button toast=(Button) findViewById(R.id.button1);

    final Spinner spinner=(Spinner) findViewById(R.id.spinner1);

         // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.Percentage, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new SpinnerActivity());

    toast.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,
                    "OnClickListener : " + 
                "\nSpinner 1 : "+    String.valueOf(spinner.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    }





<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:text="@string/Tipcalc"
    android:textSize="40sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="27dp"
    android:ems="10"
    android:hint="@string/hint"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:entries="@array/Percentage"
    android:prompt="@string/Title" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="34dp"
    android:text="Button" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

是的,同样在这里,您的代码运行正常。如果您使用extends而不是implements,通常会收到此错误。但由于OnItemSelectedListener是一个界面,你应该没问题。 也许你可以让SpinnerActivity成为MainActivity的内部类,如果它对你不起作用的话。我建议你的最后一件事就是创建一个像这样的匿名内部类:

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }

});

答案 1 :(得分:0)

也许您应该在方法@Override上方添加onItemSelected()注释。