gpt4 book ai didi

java - 长按在 ListView 行上不起作用

转载 作者:行者123 更新时间:2023-12-01 09:50:41 25 4
gpt4 key购买 nike

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

最佳答案

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

您需要在设置 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;
}

关于java - 长按在 ListView 行上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com