![](/img/trans.png)
[英]Alertdialog with a custom listview - click event is not triggered
[英]Clickable Options in AlertDialog after ListView long click
我正在尝试使用OnItemLongClickListener()设置listView,这将打开一个具有自定义布局的警报对话框。 然后,我希望布局具有两个按钮,一个用于打开另一个活动以在SQLite DB中编辑该条目,另一个用于删除该条目。 我的onItemLongClickListener()可以工作,并且我可以打开自定义布局,但是我无法弄清楚如何在单击按钮或执行所需的方法后关闭对话框。
我的ListViewItemLongClick():
private void listViewItemLongClick(){
final ListView myList = (ListView) findViewById(R.id.resourcesList);
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Id = id;
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_layout, null);
dialogBuilder.setView(dialogView);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
return true;
}
});
}
我的自定义警报布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="edit"
android:text="Edit" />
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="delete"
android:text="Delete" />
</LinearLayout>
任何帮助将不胜感激,我仍然是Android编程的新手。
干杯!
使用我的解决方案,您不需要XML文件。 当然,您可以使用它来添加一个EditText作为EditText ed = (EditText)dialog.findViewById(R.id.dialog_get_name_ed);
,并使用EditText ed = (EditText)dialog.findViewById(R.id.dialog_get_name_ed);
获得它EditText ed = (EditText)dialog.findViewById(R.id.dialog_get_name_ed);
但是对于您的情况,请尝试此
dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// you have your edit button
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// you have your delete button
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.