java - ListView 长按后 AlertDialog 中的可点击选项

标签 java android sqlite listview android-studio

我正在尝试使用 OnItemLongClickListener() 设置一个 listView,这将打开一个带有自定义布局的警报对话框。然后我希望布局有两个按钮,一个用于打开另一个 Activity 以编辑 SQLite 数据库中的条目,一个用于删除条目。我的 onItemLongClickListener() 有效,我可以打开自定义布局,但我不知道如何在单击按钮后关闭对话框,或执行我想要的方法。

我的 ListViewItemLongClick():

private void listViewItemLongClick(){
    final ListView myList = (ListView) findViewById(R.id.resourcesList);
    myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
             Id = id;


            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
            // ...Irrelevant code for customizing the buttons and title
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_layout, null);
            dialogBuilder.setView(dialogView);

            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();



            return true;
        }
    });

}

我的自定义警报布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/Edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="edit"
        android:text="Edit" />

    <Button
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="delete"
        android:text="Delete" />
</LinearLayout>

任何帮助将不胜感激,我对 android 编程还是很陌生。

干杯!

最佳答案

使用我的解决方案,您不需要 XML 文件。当然,您可以使用它来添加一个 EditText,例如使用 EditText ed = (EditText)dialog.findViewById(R.id.dialog_get_name_ed);

但是对于你的情况,试试这个

    dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // you have your edit button
        }
    });

    dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // you have your delete button
        }
    });

关于java - ListView 长按后 AlertDialog 中的可点击选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689932/

相关文章:

C# SQLite 表存在和不存在

android - Sqlite 数据库未插入在 Proguard 构建中使用脚本

java - 用 RabbitMQ 解耦同步 Web 服务层,是好主意还是坏主意?

android - 抽屉导航切换填充

android - 合并音频文件并在不同的 API 版本中播放时出现奇怪问题

android - 当应用在Android和iOS上显示为屏幕外时执行代码

python - sqlite3 是否压缩数据?

java - 按钮的新 setText 属性无缘无故出现空指针异常

java - 如何复制嵌套列表而不导致 StackOverflowError

java - 从 Java 中的对象获取泛型类型参数