Android的OnItemClickListener不工作

我正在使用android 2.3.3。 我build立了一个布局,

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/mainList" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

我正在操纵它

 package org.dewsworld.ui; import org.dewsworld.core.NBConfig; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; public class NewsBotActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter( new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, NBConfig.topics)); ListView listView = (ListView) findViewById(R.id.mainList) ; listView.setOnItemClickListener( new OnItemClickListener() { }); } } 

使用eclise IDE,当我设置OnItemClickListener时,它给了我错误

 The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new OnItemClickListener(){}) 

我无法解决这个问题。 [我添加了一个错误图像] IMGIMG

Solutions Collecting From Web of "Android的OnItemClickListener不工作"

看来你已经导入了错误的OnItemClickListener,请试试这个,并删除导入android.view.View.OnClickListener

 import android.widget.AdapterView.OnItemClickListener; 

如何通过定义onItemClick()函数来填充新对象的主体:

 public void onItemClick(AdapterView parent, View v, int position, long id) { // Display a messagebox. Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show(); } 

尝试在eclipse中使用ctrl + shift + o来自动组织所有的导入…

只需实现OnItemClickListener到你的类。

喜欢这个:

public class ClassName extends Activity implements OnItemClickListener{}