saiket
saiket

Reputation: 3063

how to capture long press event for Listeview item of a ListActivity?

lets assume i have a ListActivity , now i want catch the long press event on a list item , for that i used following block of code (get after googled) but it's not working!!! please help!

            public class InboxActivity extends ListActivity {

            this.getListView().setOnLongClickListener(new OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            Toast.makeText(InboxActivity.this, "postion: " +    getListView().getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });

            }

I also see some code in online with contextmenu but there i dont get way of getting the position of listItem from where the context menu open.

Upvotes: 29

Views: 27346

Answers (4)

user809486
user809486

Reputation: 898

I dont know if you already solved you problem, im almost sure you did, but this works

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        //Do some
        return true;
    }
});

Upvotes: 62

Idan
Idan

Reputation: 519

    (viewitem).setOnLongClickListener(new OnItemLongClickListener() {           
        @Override
        public boolean onLongClick(View v) {
            //your code here
            return false;
        }
    }

(this is a bit diffrent from the above answers)

Upvotes: 4

Dilberted
Dilberted

Reputation: 1172

i think you want to know how to get the value..

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
                    public boolean onItemLongClick(AdapterView parentView, View childView, int position, long id) {
                                            // this will provide the value
                        listView.getItemAtPosition(position)
                        return false;
                    }

                })

Upvotes: 3

acm0x
acm0x

Reputation: 775

this.getListView().setOnItemLongClickListener should work.

Upvotes: 6

Related Questions