Search code examples
androidandroid-listviewonclicklisteneronitemclicklisteneronitemclick

Android ListView onitemclick listener


when i click my list view item it calls onitemclick listener but when i click item long it calls both initemclick and onitemlongclick listeners. how to solve only call onitemlongclick listener when it long press?

     list.setOnItemClickListener(new OnItemClickListener()
           {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                //my code

            }


           });
        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//my code
    }
    }

Solution

  • Notice that, onItemLongClick() has a boolean return value. If you don't want onItemClick to be called, make onItemLongClick() to return true.

        list.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
               //....
    
               // Above are your code.
               // Return true for this method as below.
               return true;
            }
        }