开发者

how to use OnItemLongClickListener without setOnItemLongClickListener in Android?

开发者 https://www.devze.com 2023-03-30 07:19 出处:网络
I am trying to use OnItemLongClickListener for a listView on Android. This code works fine when added to onCreate method.

I am trying to use OnItemLongClickListener for a listView on Android. This code works fine when added to onCreate method.

mContactList.set开发者_开发百科OnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("MyApp", "get onItem Click position= " + position);
            return false;
        }
    });

However when I try to implement OnItemLongClickListener interface and use this method in the class:

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)   {
    Log.e("MyApp", "get onItem Click position= " + position);
    return false;
}

nothing happens. What am I missing?


Did you register your object as listener, eg. setOnItemLongClickListener(this)?


You always have to set an setOnItemLongClickListener. If you extract the onClick listener to another class than you have to set this OnItemLongClickListener to the listview.

e.g.

mContactList.setOnItemLongClickListener(new MyClassOnLogItemClickListener());

or if you are in the same class register it with this.

0

精彩评论

暂无评论...
验证码 换一张
取 消