开发者

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.


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;
    }
});


this.getListView().setOnItemLongClickListener should work.


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

(this is a bit diffrent from the above answers)


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;
                    }

                })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜