开发者

how to use OnItemLongClickListener without setOnItemLongClickListener in Android?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜