Reputation: 3267
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().
Upvotes: 1
Views: 3231
Reputation: 41099
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 the onLongClickListener
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 your Activity
as soon as you activity trying to implement onLongClickListener
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:
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
in it define your action for Long click.
Upvotes: 2
Reputation: 1046
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 your onCreate
-method.
Then you have to override the methods onCreateContextMenu
and onContextItemSelected
like so:
onCreateContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_context_layout, menu);
}
onContextItemSelected
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.my_item1:
doSomething();
return true;
case R.id.my_item2:
doSomething();
return true;
default:
return super.onContextItemSelected(item);
}
}
Upvotes: 0
Reputation: 1092
You can do basically two things:
Remove the line getListView().setOnItemLongClickListener(this);
since you didn't implemented/overrided the onItemLongClick method of AdapterView.OnItemLongClickListener
In myLV1.setOnItemLongClickListener you must create a instance of AdapterView.OnItemLongClickListener and not the View.OnItemLongClickListener
Upvotes: 1