2016-04-06 3 views
0

Я настроил OnContextItemSelected, чтобы показать предупреждение, когда пользователь пытается удалить элемент и просит их подтвердить. Проблема в том, что переменная «confirmReceived» получает значение true при первом запуске кода, когда ничего не происходит, тогда, когда он выполняется во второй раз, элемент удаляется, прежде чем пользователь сможет подтвердить, что он хочет, чтобы он удалился ,Подтверждение перед удалением из ArrayList (Android)

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); 
} 

onContextItemSelected

@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

выполнить задачу удаления в положительной кнопке нажмите ... непосредственно перед dialog.dismiss() – Sanoop

ответ

0

сделать Somthing как это

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; 
} 
+0

Я получаю сообщение «не могу разрешить символ» « – Patterrz

+0

», затем передайте элемент функции. Как AskRemoveConfirm (Элемент MenuItem) – Sanoop

+0

, а затем передать AlertDialog diaBox = AskRemoveConfirm (item); – Sanoop

Смежные вопросы