Search code examples
androidandroid-4.4-kitkatonlongclicklistener

Android 4.4 setOnItemLongClickListener bug?


I have a ListView with a setOnItemClickListener and setOnItemLongClickListener. Since 1 year no problem with it. But with Android 4.4 I'll get with a LongClick both Methods executed.

For example:

lv.setOnItemClickListener(new OnItemClickListener()
        {
        public void onItemClick(AdapterView<?> arg0,View arg1,int arg2, long arg3){
            ListView lv = (ListView) findViewById(R.id.listView1);      
            final String Name = lv.getAdapter().getItem(arg2).toString();               

            // Make sth on click
        }});


        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    final int arg2, long arg3) {
            // Make sth on longclick
            }
        });  

On long click both will be executed. Is this a bug or a problem with my code?


Solution

  • There are no problem with your code it's just that you haven't returned a value on your onItemLongClick boolean. Change it into:

        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                            final int arg2, long arg3) {
                    // Make sth on longclick
    
                    return true;
                    }
                }); 
    

    This will prevent long click on doing further actions since take note that a longclick is still a click which is why onclicklistener triggers on this event.