javaandroidandroid-listviewonlongclicklistener

java android listview setOnLongClickListener


To function long click, the override function is changed and there are only out of date references.

I am also use click listner.

but the onLongClick override funcion don't have position variable.

Can you give me recent information to make long click?

`

listView.setLongClickable(true);


listView.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        
        return false;
    }

});

`

I tried to used old codes in blogs. but the old override function is not exist any more.

And android document don't describe detail about the long click for java.


Solution

  • I would recommend you to use RecyclerView which is newer and performs better than Listview on large datasets.

    However if you wanna keep using ListView then you want to use its setOnItemLongClickListener to listen long clicks on the list items. The following snippet shows how it is used:

    ListView yourListView;
    
    yourListView = findViewById(R.id.listview);
    
    yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener{
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            // Here you will have all the information you need:
            // parent is listview, view is item view that is clicked,
            // position is the item's position in the adapter, id is given automatically I think
        }
    });