Android

ListViewで削除確認ダイアログを表示する方法

やりたかったこと

ListViewのitemを長押し
→確認ダイアログを表示
→「OK」のときだけitemを削除する

解決法

  • onItemLongClickの中でダイアログを作る
// listViewはMainActivity内のListViewとする
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("削除しますか?")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // remove item from ArrayList
                        data.remove(position);
                        // update ListView
                        adapter.notifyDataSetChanged();
                        Toast.makeText(MainActivity.this, "削除しました", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("キャンセル", null)
                .setCancelable(true);
        // show dialog
        builder.show();
         return false;
    }

公式のやり方

https://developer.android.com/reference/android/app/DialogFragment.html
より抜粋

  • 呼び出し元のActivityに各Buttonをクリックしたときの処理をメソッドとして書いておく
  • DialogFragment内では、getActivity().doPositiveButton()で、そのメソッドを呼ぶだけ
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(title)
            .setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doPositiveClick();
                    }
                }
            )
            .setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doNegativeClick();
                    }
                }
            )
            .create();
}

追記: カスタムadapterを使ったときの、itemの削除方法

https://stackoverflow.com/questions/7372301/how-do-i-delete-an-item-from-my-custom-base-adapter

You do not delete from the adapter ! You delete from the items ! and the adapter is between your items and the view. From the view you can get the position and according the position you can delete items. Then the adapter will refresh you views.

That means you need to do something like this

items.remove(position);
adapter.notifyDataSetChanged()