2016-04-06 19 views
0

Kullanıcı bir öğeyi silmeye çalıştığında ve onay için sorulduğunda bir uyarı göstermek için OnContextItemSelected öğesini kurdum. Sorun, "confirmationReceived" değişkeninin, kodun başka hiçbir şey olmadan yürütüldüğü ilk seferde doğru olarak ayarlanmasıdır. Bu durumda, kullanıcı silindikten sonra kullanıcının silinmesini istediğini onaylamadan önce ikinci kez uygulandığında öğe silinir .ArrayList (Android) uygulamasından silmeden önce onay

onCreateContextMenu

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    menu.setHeaderTitle("Timetable Item"); 
    menu.add(0, Remove_Item, Menu.NONE, R.string.Remove_Item); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    super.onContextItemSelected(item); 
    AlertDialog diaBox = AskRemoveConfirm(); 
    diaBox.show(); 
    switch (item.getItemId()) { 
     case (Remove_Item): { 

      if(confirmationReceived == true) { 
       AdapterView.AdapterContextMenuInfo menuInfo; 
       menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
       int index = menuInfo.position; 
       removeItem(index); 
       confirmationReceived = false; 
       return true; 
      } 
     } 
    } 
    return false; 
} 

AskRemoveConfirm()

private AlertDialog AskRemoveConfirm() 
{ 
    AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this) 
      .setTitle("Confirmation") 
      .setMessage("Are you sure you want to delete this entry?") 
      .setPositiveButton("Delete", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        confirmationReceived = true; 
        dialog.dismiss(); 
       } 

      }) 
      .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }) 
      .create(); 
    return myRemovalDialogBox; 
} 
+0

, pozitif düğmeyi kaldır görevini tıklatırsanız hemen önce dialog.dismiss() – Sanoop

cevap

0

bu

private AlertDialog AskRemoveConfirm() 
    { 
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this) 
     .setTitle("Confirmation") 
     .setMessage("Are you sure you want to delete this entry?") 
     .setPositiveButton("Delete", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       confirmationReceived = true; 
       if(confirmationReceived == true) { 
       AdapterView.AdapterContextMenuInfo menuInfo; 
       menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
       int index = menuInfo.position; 
       removeItem(index); 
       confirmationReceived = false; 
       return true; 
       } 
       dialog.dismiss(); 
      } 

     }) 
     .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }) 
     .create(); 
return myRemovalDialogBox; 
} 
gibi yapmak somthing onContextItemSelected
+0

"Simgeyi çözemedim '' öğesi '" – Patterrz

+0

öğeyi işleve iletiyorum. Beğen AskRemoveConfirm (MenuItem öğesi) – Sanoop

+0

ve sonra AlertDialog diaBox = AskRemoveConfirm (item); – Sanoop