尝试在 ListView 中设置 onItemClickListener 时出错

error while trying to set onItemClickListener in ListView up

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();
        }
    }
});

Error:(52, 13) error: method getOnItemClickListener in class AdapterView cannot be applied to given types; required: no arguments found: reason: actual and formal argument lists differ in length where T is a type-variable: T extends Adapter declared in class AdapterView

尝试这样做

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

你应该将 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
      }
   });
chHE.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
        if(position==0){
            toast.show();
        }
    }
});

也许你写了一个mastake?更改get=>set