android開發中如何使用 alertDialog從listView中刪除數據?

我現在使用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人 整理髮布,轉載須標明出處。

發佈了296 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章