2014-03-12 55 views
0

我正在使用android。我想删除我选择了特定的按钮..我用onContextItemSelected用于选择button.What我写里面如何通过选择特定按钮来删除数据

setPositiveButton的public void onClick(DialogInterface dialog,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(); 
       } 
+0

哪里是在你的代码的按钮实例? – Shubhank

回答

2

如果你已经在你的XML布局定义的按钮,你不能删除它,但你可以从视图通过设置将其删除(这是最常见的情况):

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

的上面的行会进入你的public void onClick(DialogInterface对话框,int id){}。

如果你的代码中动态添加的按钮,你可以通过获取父布局,并做其删除:

ViewGroup.removeView(button); 
相关问题