appukrb
appukrb

Reputation: 1507

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
    }
    }

Upvotes: 1

Views: 2708

Answers (4)

GK_
GK_

Reputation: 1222

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

Upvotes: 0

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24453

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

Upvotes: 0

TieDad
TieDad

Reputation: 9939

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

Upvotes: 2

mainu
mainu

Reputation: 448

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

Upvotes: 0

Related Questions