2017-10-12 83 views
0

我有這個ListView包含項目,我想創建一個提醒對話框,當我長時間點擊一個項目時刪除這些項目。 onLongClick對一個項目顯示一個AlertDialog,如果我單擊是,它將刪除該項目。使用提示對話框從ListView中刪除一個項目

這是我的代碼。

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 
     new AlertDialog.Builder(MainActivity.this) 
       .setIcon(android.R.drawable.ic_dialog_alert) 
       .setMessage("Are You Sure You Want to Delete This Note?!") 
       .setTitle("Attempt to Delete A Note") 
       .setPositiveButton("YES", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         try { 
          notesList.remove(i); 
          arrayAdapter.notifyDataSetChanged(); 

          Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show(); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       }) 
       .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show(); 
        } 
       }) 
       .show(); 
     return true; 
    } 
}); 
+1

那麼什麼是您所遇到的問題? –

+0

描述您的問題。當您按YES按鈕時,該項目是不會被刪除? –

+0

當我選擇是刪除該項目時,應用程序崩潰 –

回答

3

我覺得問題是'我'的位置提醒對話框點擊列表和你需要用戶列表項目點擊的位置,以便從列表中刪除項目。

請使用如下代碼:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) { 
      new AlertDialog.Builder(MainActivity.this) 

        .setIcon(android.R.drawable.ic_dialog_alert) 
        .setMessage("Are You Sure You Want to Delete This Note?!") 
        .setTitle("Attempt to Delete A Note") 
        .setPositiveButton("YES", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          try { 
           if(notesList!=null){ 
           notesList.remove(position); 
           arrayAdapter.notifyDataSetChanged(); 

          Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show(); 
          } 
          }catch (Exception e){ 
           e.printStackTrace(); 
          } 
         } 
        }) 
        .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show(); 
         } 
        }) 
        .show(); 
      return true; 
     } 
    }); 
0
listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
      new AlertDialog.Builder(MainActivity.this) 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .setMessage("Are You Sure You Want to Delete This Note?!") 
        .setTitle("Attempt to Delete A Note") 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          try { 
           dataModels.remove(position); 
           adapter.notifyDataSetChanged(); 

           Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show(); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         } 
        }) 
        .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show(); 
         } 
        }) 
        .show(); 

     } 
    }); 

這是我的作品嚐試這種