I'm trying to enable Long Clicks in my app and I can't get it to compile. I looked at the examples from previous questions here, but I can't get any of them to compile. My main activity is declared as:
import android.widget.AdapterView.OnItemLongClickListener;
public class LinearLayoutDemo extends ListActivity implements OnClickListener, OnItemClickListener, OnItemLongClickListener {
In the onCreate() method, I put this:
getListView().setOnItemLongClickListener(this);
And for my ListView (myLV), I did this:
myLV1.setOnItemLongClickListener(new View.OnItemClickListener() {
@Override public boolean onLongClick(View v) {
Log.d(TAG, "setOnItemLongClickListener() called for myLV");
return(true);
}
});
I think the above must be wrong, but I don't know why. The compiler error is
LinearLayoutDemo.java:45: com.commonsware.android.linearpct.LinearLayoutDemo is not abstract and does not override abstract method onItemLongClick(android.widget.AdapterView<?>,android.view.View,int,long) in android.widget.AdapterView.OnItemLongClickListener
[javac] public class LinearLayoutDemo extends ListActivity implements OnClickListener, OnItemClickListener, OnItemLongClickListener {
LinearLayoutDemo.java:287: cannot find symbol
[javac] symbol : class OnItemLongClickListener
[javac] location: class android.view.View
[javac] myLV1.setOnItemLongClickListener(new View.OnItemLongClickListener() {
[javac] ^
*************** UPDATE ***************
I got rid of OnItemLongClickListener in the ListActivity class definition. I also got rid of this
getListView().setOnItemLongClickListener(this);
Then I added this code and it worked ( I don't know why):
myLV1.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG, "onItemLongClick() for LV1");
return true;
}
});
It seems pretty simple, so I'm sticking with it, unless someone can tell me I shouldn't do it like this.
************ EDIT **************
Okay, I have the Long Clicks working. How can I tell which item I long-clicked? I had assumed that myLV1.getCheckedItemPosition() would work, but it doesn't.
Nevermind, it's in 'position' parameter to onItemLongClick().
What you are doing here is practicly setting the onLongClickListener twice:
1.
getListView().setOnItemLongClickListener(this);
here you set the
Activity
to be the Listener as it implements theonLongClickListener
interface.2.
myLV1.setOnItemLongClickListener(new View.OnItemClickListener() {....
Here you are creating a new
onItemClickListener
and trying to apply it again to the list. remove this part from your activity code.and add unimplemented method:
onLongClick
that should be in yourActivity
as soon as you activity trying to implementonLongClickListener
In this method you can specify what are the actions in case of a long click.
UPDATE:
This will add you this code to the class:
in it define your action for Long click.
You can do basically two things:
Remove the line
getListView().setOnItemLongClickListener(this);
since you didn't implemented/overrided the onItemLongClick method of AdapterView.OnItemLongClickListenerIn myLV1.setOnItemLongClickListener you must create a instance of AdapterView.OnItemLongClickListener and not the View.OnItemLongClickListener
Do you want to create a context-menu on long-clicking a ListView-Item?
If so the easiest way to achieve this is by using
registerForContextMenu(findViewById(android.R.id.list));
inside youronCreate
-method.Then you have to override the methods
onCreateContextMenu
andonContextItemSelected
like so:onCreateContextMenu
onContextItemSelected