尝试在ListView中设置onItemClickListener时出错

时间:2017-07-14 10:31:19

标签: android listview listviewitem

error screenshot

我正在尝试在ListView中制作可点击的项目,并重新发现此类错误。

ListView chHE = (ListView) findViewById(R.id.lvHE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.support_simple_spinner_dropdown_item, cities);
chHE.setAdapter(adapter);

chHE.getOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long l) {
        if (position == 0) {
            toast.show();
        }
    }
});
  

错误:(52,13)错误:类AdapterView中的方法getOnItemClickListener   不能适用于给定的类型;   必需:没有参数   发现:   原因:实际和正式的参数列表长度不同   其中T是一个类型变量:   T扩展了AdapterView类中声明的Adapter

3 个答案:

答案 0 :(得分:0)

您应该将getOnItemClickListener重新加入setOnItemClickListener

ListView lv = getListView();
   lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
            // assuming string and if you want to get the value on click of list item
            // do what you intend to do on click of listview row
      }
   });

答案 1 :(得分:0)

pow

也许你已经写过一个mastake?change get =&gt; set

答案 2 :(得分:-1)

尝试这样做

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //doSomeThing
    }
});