如果用户从Spinner下拉菜单中选择Admin,则此活动将移至About.class,当用户从Spinner下拉菜单中选择Teacher时,此活动将移至Student.class,但会发生错误。 错误来自以下几行:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
描述资源路径位置类型 AdapterView类型中的方法setOnItemSelectedListener(AdapterView.OnItemSelectedListener)不适用于参数(new OnItemSelectedListener(){})Home.java / StudentRecord / src / edu / riphah / fun / StudentRecord line 40 Java问题
Home.java代码:
public class Home extends Activity implements OnClickListener{
Button Btn,Btn1;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
TextView textView2 = (TextView) findViewById(R.id.textview2);
String delegate = "MMM dd, yyyy h:mm aa";
java.util.Date noteTS = Calendar.getInstance().getTime();
textView2.setText("Time:"+DateFormat.format(delegate,noteTS));
init();
String[] sp1 = getResources().getStringArray(R.array.Level);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sp1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s=((TextView)view).getText().toString();
if(s.equals("Admin"))
startActivity(new Intent(view.getContext(),About.class));
if(s.equals("Teacher"))
startActivity(new Intent(view.getContext(),Student.class));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
字符串代码:
<string-array name="Level">
<item>Admin</item>
<item>Teacher</item>
<item>Dean</item>
</string-array>
XML代码:
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="14dp"
android:textColor="#FFFFFFFF"
android:entries="@array/Level" />
答案 0 :(得分:0)
您可能导入了错误的课程
确保您正在导入
android.widget.AdapterView.OnItemSelectedListener
或更改行
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
到
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
修改强> 你应该改变
String s=((TextView)view).getText().toString();
到
String s = parent.getItemAtPosition(position).toString();