【问题标题】:How to delete item from listView using alertDialog如何使用 alertDialog 从 listView 中删除项目
【发布时间】:2014-04-12 19:18:20
【问题描述】:

我有一个 ListView 显示不同配置文件的名称,当用户点击一个项目时,活动显示一个 alertDialog,如果用户按下确认按钮,我想从我的 listView 中删除该元素,从我的 ArrayAdapter 和来自我的 ArrayList。 我知道 onItemLongClick 方法中的 arg2 表示所选项目的索引,但我希望能够在肯定按钮的 onClick 方法中访问它。 有什么建议吗? 我的 ArrayList 称为“ListaUtentiStringa”,ArrayAdapter 是“profilesAdapter”,listView 称为 listview。 对不起,我的英语不好。

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setTitle("Vuoi davvero cancellare il profilo?");
        builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                          // How to remove the selected item?
                            }

                        });

builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {

        }

        });

        AlertDialog alert = builder.create();
        alert.show();
        profilesAdapter.notifyDataSetChanged();


    return true;
    }
    }); 

【问题讨论】:

  • 您可以将项目的位置传递给警报对话框,如果用户按下删除按钮,那么您只需删除该位置的项目!
  • 如何通过该职位?我无法通过 arg2

标签: android listview android-listview onclick


【解决方案1】:

这样做:

   listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
              final int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(true);
        builder.setTitle("Vuoi davvero cancellare il profilo?");
        builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
         // How to remove the selected item?
          adapter.remove(adapter.getItem(position));
        }

    });

【讨论】:

  • 我无法引用位置变量,我写了我在其中一个 cmets 中遇到的错误
【解决方案2】:

试试这个..

使用ListaUtentiStringaArrayListprofilesAdapteradapter作为全局变量

 builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                                      // How to remove the selected item?

                                ListaUtentiStringa.remove(arg2);
                                profilesAdapter.notifyDataSetChanged();
                                dialog.dismiss();
                        }

                    });

编辑

public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {

【讨论】:

  • 但我不能引用 arg2,因为“不能引用在不同方法中定义的内部类中的非最终变量 arg2”
  • @Moltehh 使用View arg1,final int arg2, long arg3
  • 如果我尝试使用 arg2,我会收到此错误(“无法在以不同方法定义的内部类中引用非最终变量 arg2”)
  • 刚刚看到您的编辑;现在没有编译错误,也许它可以工作。谢谢!
  • @Moltehh 试试看。它应该工作。如果它有效,请不要忘记接受我的回答。
猜你喜欢
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2014-10-29
相关资源
最近更新 更多