gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:05:04 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com