0

ListView のセルを長押しして、AlertDialog がポップアップし、ListView の行を削除できるようにしたいと考えています。ただし、長いクリックは登録されません。私も設定してみました

 userChatroomListView.setLongClickable(true)

以下はコードです。

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

xmlでもそうしました。私は今何をしますか?

xml コードは次のとおりです。

<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>
4

2 に答える 2

0

質問自体には関係ありませんが、同じ問題がある場合は、 を使用していることと、 を使用OnItemLongClickListenerしていないことを確認してくださいsetOnLongClickListener

于 2020-07-30T15:09:25.997 に答える