java - 如何通过选择特定按钮来删除数据

标签 java android

我正在使用Android。我想删除我选择的特定按钮。我正在使用 onContextItemSelected 来选择按钮。我在里面写什么

setPositiveButton 的 public void onClick(DialogInterfacedialog,int id) {}??

 @Override  
    public boolean onContextItemSelected(MenuItem item)
    {  

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        String number;
        final Context context = this;

        try
        {
                //number=new ContactListAdapter (this).numberList.get(info.position);



              if(item.getTitle()=="View ")
              {
                Dialog dialog=new Dialog(HubActivity.this);
                dialog.setContentView(R.layout.driver_details);

                dialog.setTitle("Driver Details");
                dialog.show();

              }  
              else if(item.getTitle()=="Edit ")
              {
                Dialog dialog=new Dialog(HubActivity.this);
                dialog.setContentView(R.layout.activity_driver);
                dialog.setTitle("Edit Details");
                dialog.show();

              }
              else if(item.getTitle()=="Delete ")
              {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                    // set title
                    alertDialogBuilder.setTitle("Delete");

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("Are you sure to delete ?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                //MainActivity.this.finish();
                            }   
                          })
                        .setNegativeButton("No",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
              }

最佳答案

如果您在 xml 布局中定义了按钮,则无法删除它,但可以通过设置将其从 View 中删除(这是最常见的情况):

// Button button = findViewById(R.id.button);
button.setVisibility(View.GONE);

上面的行将进入您的 public void onClick(DialogInterfacedialog,int id) {}。

如果您在代码中动态添加了按钮,则可以通过获取父布局并执行以下操作来删除它:

ViewGroup.removeView(button);

关于java - 如何通过选择特定按钮来删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360749/

相关文章:

java - 如何正确同步两个线程

java - 将运行时错误的堆栈跟踪打印到文件中

java - Android - 将 TCP 客户端转换为 UDP 客户端

android - 奖励视频广告-无法加载广告错误代码 3 admob

javascript - 如何在 android/三星手机的 html 文本输入字段中停止自动大写?

java - 如何使用在所有操作系统中兼容的 FileInputStream 读取文件

java - Eclipse 不导入 Android 项目

java - 运行 mahout 时线程 "main"java.lang.NullPointerException 中的异常

java - 自定义 ArrayAdapter 未被调用

android - 在 Android 中命名资源最清晰的方法是什么?