1

I'm a little bit confused how to handle a long click on a seperate listview item and then let a popup show for the item.

So here's my code, please ask some questions if you don't understand something.

MainActivity.java

ListView listView = (ListView) findViewById(R.id.listView);

listView.setOnLongClickListener(new View.OnLongClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public boolean onLongClick(View v) {
                PopupMenu p = new PopupMenu(MainActivity.this, v);
                p.getMenuInflater().inflate(R.layout.popup_layout, p.getMenu());
                p.show();
                return true;
            }
        });

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <menu
        android:layout_width="185dp"
        android:layout_height="141dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="96dp"
        tools:layout_editor_absoluteY="83dp">

        <item
            android:id="@+id/order_takeout"
            android:onClick="doTakeOut"
            android:title="@string/order_takeout"
            android:layout_width="delete" />

        <item
            android:id="@+id/order_eat_in"
            android:onClick="doEatIn"
            android:title="@string/edit" />
    </menu>
</androidx.constraintlayout.widget.ConstraintLayout>

viewcontents_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp"
        android:longClickable="true"
        tools:ignore="MissingConstraints"/>

</androidx.constraintlayout.widget.ConstraintLayout>

If anyone can help me out there I am very happy for an answer. Stay healthy!

Noah
  • 188
  • 4
  • 24

2 Answers2

2

your wrong is using setOnLongClickListener you can use :

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {


            return true;
        }
    }); 
AAV
  • 798
  • 1
  • 6
  • 23
  • If I do this, I get an error on the (new AdapterVie.onItemLongClickListener) Class 'Anonymous class derived from OnLongClickListener' must either be declared abstract or implement abstract method 'onLongClick(View)' in 'OnLongClickListener' It says, i have to use public boolean onLongClick(View v) Probably another solution of your side? – Noah Oct 07 '20 at 06:41
  • Now i get an Exception: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemLongClickListener(android.widget.AdapterView$OnItemLongClickListener)' on a null object reference on the following line: `listView.setOnItemLongClickListener(new OnItemLongClickListener() {` – Noah Oct 07 '20 at 08:18
0

So I solved the problem.

  1. Using another java class called ViewListContents and changed the place where my popup_layout is, I moved it into the menu and set some menu tags and not constraint tags.

  2. I've change the R.layout to R.menu in my onItemLongClickListener

In this class I used it like this:

    listView = (ListView) findViewById(R.id.listView);

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            PopupMenu p = new PopupMenu(ViewListContents.this, v);
            MenuInflater inflater = p.getMenuInflater();
            inflater.inflate(R.menu.popup_layout, p.getMenu());
            p.show();
            return true;
        }
    });
Noah
  • 188
  • 4
  • 24