在上下文菜单上选择选项时调用确认对话框

calling a confirmation dialog when a option is selected on a context menu

我有下面的代码,当长按列表视图上的项目时调用对话框。

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Projeto clickedProjeto = mAdapter.getItem(position);
        Log.d(TAG, "longClickedItem : "+ clickedProjeto.getName());
        showDeleteDialogConfirmation(clickedProjeto);
        return true;
    }

现在我需要调用完全相同的 showDeleteDialogConfirmation() 方法。但是我不知道如何将点击的项目传递给对话框。

这是所选项目的上下文菜单。我需要在 Excluir 语句上调用对话框。

@Override
    public boolean onContextItemSelected(MenuItem item){
        if(item.getTitle()=="Editar"){
            Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
        }
        else if(item.getTitle()=="Excluir"){
            //here instead of this toast I need to call the confirmation dialog.
            Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
        }else{
            return false;
        }
        return true;
    }

删除:

implements OnLongClickListener,

并添加这个

myList.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> parent,
                        View view, int position, long id) {
                Projeto clickedProjeto = (Projeto)parent.getItem(position);
                Log.d(TAG, "longClickedItem : "+ clickedProjeto.getName());
                showDeleteDialogConfirmation(clickedProjeto);
                return false;
                }

            });

已解决。获取在列表中单击的项目的索引并将其传递给对话框。

@Override
    public boolean onContextItemSelected(MenuItem item){
        if(item.getTitle()=="Editar"){
            Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
        }
        else if(item.getTitle()=="Excluir"){
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            int index = info.position;
            Projeto clickedProjeto = mAdapter.getItem(index);
            showDeleteDialogConfirmation(clickedProjeto);
        }else{
            return false;
        }
        return true;
    }