1

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
    }
    }
appukrb
  • 1,507
  • 4
  • 24
  • 53

4 Answers4

2

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;
        }
    }
TieDad
  • 9,143
  • 5
  • 32
  • 58
0

In Such case better to use onClickListener() for individual views of the List instead of the list. And also for onItemLongClickListener() for the Views.

mainu
  • 448
  • 2
  • 11
0

Try using return like this...

list.setOnItemClickListener(new OnItemClickListener() {

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



        }


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

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
    return true;
}
}
GK_
  • 1,212
  • 1
  • 12
  • 26
0

You just need to return false to tell system that it shouldn't deliver the event any more.

  list.setOnItemLongClickListener(new OnItemLongClickListener() {

         public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
              //Do something
              return false;
        }
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165