gpt4 book ai didi

java - 为微调器实现 onItemSelectedListener

转载 作者:行者123 更新时间:2023-11-30 03:01:29 26 4
gpt4 key购买 nike

出现这个错误:

"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>

最佳答案

是的,同样在这里,你的代码工作正常。如果您使用 extends 而不是 implements,通常会出现此错误。但是因为 OnItemSelectedListener 是一个接口(interface),所以你应该没问题。也许您可以将 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

}

});

关于java - 为微调器实现 onItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462339/

26 4 0