0

I'm trying to implement a long click in my listview item but it doesn't work and i get an error that says is undefined. Here's the code:

protected void setOnItemLongClickListener(ListView l, View v, int position, long id) {
        super.onItemLongClick(l, v, position, id);// Error

        ApplicationInfo app = applist.get(position);
        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
            } catch (ActivityNotFoundException e) {
                Toast.makeText(MainActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

    }

Someone has an idea how solve the problem?Thanks

David_D
  • 1,404
  • 4
  • 31
  • 65

5 Answers5

1

The reason for this is most likely that you don't implement the listener. Something like

public class ActivityName extends Activity implements OnItemLongClickListener{

Try changing

protected void setOnItemLongClickListener

to

protected boolean setOnItemLongClickListener{
      // your code
      return true;

You need to use the proper return type for the method which is boolean then return true so the listener knows it was a success.

Docs

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • i've already written the implements but nothing.. Always `The method onItemLongClick(ListView, View, int, long) is undefined for the type ListActivity` – David_D Oct 23 '13 at 12:45
  • @David_D without seeing more of your code, I would suggest cleaning "Project --> Clean..." and see if that helps. Also, add the `@Override` annotation before the method. You may also copy the entire method, delete it, and paste back in. I've had trouble with Eclipse before where I needed to do that if I didn't do everything in the order it wanted. – codeMagic Oct 23 '13 at 13:03
0

Please replace

public class MainActivity extends Activity  implements OnItemLongClickListener

and add unimplemented method

@Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        return false;
    }

by default you can do this by right clicking on OnItemLongClickListener select Quick fix

Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • same problem.. the error is: `The method onItemLongClick(ListView, View, int, long) is undefined for the type ListActivity` – David_D Oct 23 '13 at 12:43
  • replace this public class MainActivity extends Activity implements OnItemLongClickListener add its unimplemented methods @Override public boolean onItemLongClick(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub return false; } – Gajendra Rawat Oct 23 '13 at 12:51
  • what i don't understand is; have i to put my entire code inside this method? – David_D Oct 23 '13 at 13:00
  • maybe i wrong something... can you take my code and show me what i have to do exactly? i get a lot errors :( also in the rest of activity – David_D Oct 23 '13 at 13:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39842/discussion-between-morroko-and-david-d) – Gajendra Rawat Oct 23 '13 at 16:42
0

try this listener for Listview :

istView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

use this code

yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            //YOUR_CODE_HERE

            return false;
        }
    });
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0

try to add this lines to your list adapter

        view.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });

and the method is try to overwrite your method

@Override
public boolean onItemLongClick(
        AdapterView<?> parent, View view,
        int position, long id) {
    // TODO Auto-generated method stub
    return false;
}   
steevoo
  • 621
  • 6
  • 18