10

I have below codes:

public class MainActivity extends ListActivity { 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}   
protected void onListItemClick(ListView l, View v, final int position, long id) {
    super.onListItemClick(l, v, position, id);
}}

I need to change this to onListItemLongClick() but how? Is it possible?

N J
  • 27,217
  • 13
  • 76
  • 96
Mostafa Fahimi
  • 510
  • 1
  • 8
  • 23
  • Could you try explaining what you want to do in a different way, with more detail? Your example code doesn't seem to accomplish anything, so it's hard to guess... – Dan Getz Jul 05 '15 at 05:32
  • I need a function that when i click long time (like 2 second) on an item in listview then my other codes in function execute. – Mostafa Fahimi Jul 05 '15 at 05:35
  • 1
    have you tried http://stackoverflow.com/questions/8846707/how-to-implement-a-long-click-listener-on-a-listview ? – Roi Divon Jul 05 '15 at 05:40
  • Yeah i saw this but i need to do that as a function.watch my example function. I need exactly like that but with longclick. – Mostafa Fahimi Jul 05 '15 at 05:44

5 Answers5

6

Your question is very similar to this one, but it looks like it's not an exact duplicate.

What you've noticed is that the ListActivity class does not have a method override specifically for this case.

In order to add this functionality as a method override, your class should implement the AdapterView.OnItemLongClickListener interface, and then you can add the onItemLongClick() method override, which acts just as the onListItemClick() method override you already have, but responds to long clicks.

Just make sure that you follow instructions from this answer, you must use android:longClickable="true" in the layout xml, or call listview.setLongClickable(true);

Example:

public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {

    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listview = (ListView) findViewById(R.id.list);

        listview.setLongClickable(true);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> l, View v,
                                   final int position, long id) {

        Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();

        return true;
    }

    protected void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);

        Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();  

    }

 //....................
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
3

you can simply do it with setOnItemLongClickListener

listview.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(ClassName.class, "Long Clicked Trigger: ", Toast.LENGTH_LONG).show();
            return true;
        }
});
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
2

try this

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                int index, long arg3) {

             // write your code

            return false;
        }}); 
Kumar Saurabh
  • 2,297
  • 5
  • 29
  • 43
SUNIL GOWROJI
  • 143
  • 1
  • 2
  • 12
1

You could implement the listener, then the callback would appear as a function (method) in your class:

public class MainActivity extends ListActivity implements View.OnLongClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View listView = findViewById(R.id.list_view);
        listView.setOnLongClickListener(this);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    @Override
    public boolean onLongClick(View v) {
        // Do your work here
        return false;
    }

}
Simas
  • 43,548
  • 10
  • 88
  • 116
-1

yes it is possible and quite easy to achieve, simply you have to make the instance of the listview like

ListView lv = (ListView)findViewById(R.id.listview);
lv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View 
                    // your code here
                    return true;
                }

        });)

in your case instead of extending ListActivity you can make a list view in xml and find the view in MainActivity and follow the above step.

Maniya Joe
  • 761
  • 6
  • 24