我正在尝试使用 OnItemLongClickListener() 设置一个 listView,这将打开一个带有自定义布局的警报对话框。然后我希望布局有两个按钮,一个用于打开另一个 Activity 以编辑 SQLite 数据库中的条目,一个用于删除条目。我的 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);
但是对于你的情况,试试这个
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
}
});
关于java - ListView 长按后 AlertDialog 中的可点击选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43689932/