setOnItemClickListener在ListView活动中给出编译错误

时间:2012-10-30 16:39:37

标签: android listview

我一直在关注ListView的教程,以下代码给出了错误。我搜索了所有可以找到的论坛,但我不断提出相同的推荐代码。也许所有的论坛都引用旧版本,也许Android已经开始了。

无论如何,这是代码和错误消息:

 getListView().setOnItemClickListener(new OnItemClickListener()
 {
 //@Override
 public void onItemClick(AdapterView<?> parent, View view,
 int position, long id) 
 {
 // TODO Sort out error and add function code
 }
 });

错误讯息:

 Multiple markers at this line

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

 -OnItemClickListener cannot be resolved to a type

任何优惠?

3 个答案:

答案 0 :(得分:7)

你应该能够通过像这样指定 onItemClickListener 的父类来解决这个问题:

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

答案 1 :(得分:0)

试试这个

ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener()
 {
 //@Override
 public void onItemClick(AdapterView<?> parent, View view,
 int position, long id) 
 {
 // TODO Sort out error and add function code
 }
 });

答案 2 :(得分:0)

我建议如此,如果有ListView,extends ListActivity并处理listitem,请点击onListItemClick

@Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);
        //handle listitem click.
    }