android开发中如何使用 alertDialog从listView中删除数据?

本文介绍如何在Android应用中使用ListView结合AlertDialog实现长按删除功能。通过设置OnItemLongClickListener监听器,在弹出确认对话框并点击确定后,从ArrayList中移除所选元素,并通知Adapter更新视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我现在使用listView展示了很多的配置信息,我现在想在点击其中一条的时候填出 alertDialog,点击确认后就删除该条数据,( ArrayAdapter ,ArrayList,listView 全部删除),我知道在 下面的onItemLongClick 方法中 参数 arg2  是选中的序号,但是我不知道如何继续处理下去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 ;
  
     }
  
     });

处理方法

 

试下这个
设置  ListaUtentiStringa ArrayList、 profilesAdapter adapter 为全局变量

1
2
3
4
5
6
7
8
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();
                          }
                     });

 


原文地址:http://www.itmmd.com/201411/125.html 
该文章由 萌萌的IT人 整理发布,转载须标明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值