0

I want to be able to long click ListView cells so that an AlertDialog pops up and lets me delete a row in the ListView. However, the long click does not register. I even tried setting

 userChatroomListView.setLongClickable(true)

below is the code.

 userChatroomListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(UserInfoActivity.this);
            final String roomName = ((TextView) view).getText().toString();
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete " + roomName);
            final int positionToRemove = i;
            adb.setNegativeButton("No", null);
            adb.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    userChatrooms.remove(positionToRemove);
                    userRoomsRef.child(roomName).removeValue();
                    BaseAdapter adapter = (BaseAdapter) userChatroomListView.getAdapter();
                    adapter.notifyDataSetChanged();
                }
            });
            return false;
        }

and also did so in xml. What do I do now?

Here's the xml code:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ChatroomListTextView"
    android:layout_alignParentBottom="true"
    android:id="@+id/userChatroomsListView"
    android:background="#29dcc4"
    android:longClickable="true"
    >
</ListView>
Abhi V
  • 714
  • 1
  • 4
  • 19

2 Answers2

1

I believe that your issue is that you are not showing the dialog.

You need to add adb.show() after setting the onclick listener and before returning.

        userChatroomListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder adb = new AlertDialog.Builder(UserInfoActivity.this);
            final String roomName = ((TextView) view).getText().toString();
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete " + roomName);
            final int positionToRemove = i;
            adb.setNegativeButton("No", null);
            adb.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    userChatrooms.remove(positionToRemove);
                    userRoomsRef.child(roomName).removeValue();
                    BaseAdapter adapter = (BaseAdapter) userChatroomListView.getAdapter();
                    adapter.notifyDataSetChanged();
                }
            });
            adb.show();
            return false;
    }
MikeT
  • 51,415
  • 16
  • 49
  • 68
0

Not relevant to the question itself, but if you have the same problem, check that you're using OnItemLongClickListener and not setOnLongClickListener.

Sudix
  • 350
  • 2
  • 15