【问题标题】:Long Click Does Not Work On ListView rows长按不适用于 ListView 行
【发布时间】:2016-06-04 02:05:41
【问题描述】:

我希望能够长按 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>

【问题讨论】:

  • 请显示行 html。
  • xml for row not listview,请
  • 对不起,我不确定你的意思。我不知道该行的xml在哪里?我有用于创建行的 Java 代码
  • 哦,您的项目行布局中有哪些元素?请确保它们不能聚焦、可点击或长时间点击
  • adb.show() 似乎不见了(我自己总是这样做:))

标签: java android listview long-click onitemlongclicklistener


【解决方案1】:

我认为您的问题是您没有显示对话框。

您需要在设置onclick监听器之后和返回之前添加adb.show()

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

【讨论】:

  • 糟糕!多么容易解决!谢谢。
【解决方案2】:

与问题本身无关,但如果您有同样的问题,请检查您使用的是OnItemLongClickListener,而不是setOnLongClickListener

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多