【问题标题】:Clickable Options in AlertDialog after ListView long clickListView长按后AlertDialog中的可点击选项
【发布时间】:2017-09-27 03:31:01
【问题描述】:

我正在尝试使用 OnItemLongClickListener() 设置一个 listView,这将打开一个带有自定义布局的警报对话框。然后我希望布局有两个按钮,一个用于打开另一个活动以编辑 SQLite DB 中的条目,另一个用于删除该条目。我的 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 编程还是很陌生。

干杯!

【问题讨论】:

    标签: java android sqlite listview android-studio


    【解决方案1】:

    使用我的解决方案,您不需要 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
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多