Skip to content Skip to sidebar Skip to footer

Debug Tells Me To Switch SetOnClickListener With SetOnItemClickListener

When debugging, the console tells me I should use setOnItemClickListener instead of setOnClickListener because I have an AdapterView. This error comes up when I press a button and

Solution 1:

Setting an OnClickListener on an AdapterView doesn't make sense because generally its the children of the AdapterView that should be clicked (e.g. rows of a ListView). That's why it's telling you to set an OnItemClickListener instead.

When you do that, your anonymous inner class needs to change because OnItemClickListener does not have a method with the signature void onClick(View v). You need

findViewById(R.id.button4).setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // your code here
    }
});

Post a Comment for "Debug Tells Me To Switch SetOnClickListener With SetOnItemClickListener"