【发布时间】:2017-09-27 03:31:01
【问题描述】:
我正在尝试使用 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 编程还是很陌生。
干杯!
【问题讨论】:
标签: java android sqlite listview android-studio