![](/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.