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
.
精彩评论