询问对话框以从列表视图中删除项目

时间:2017-11-17 20:52:11

标签: java android-studio

我有这个代码非常适合从listview中删除一个项目并且它会消失。我想显示对话框,询问用户是否要删除项目。

public void ShowdatainlstView() {
    Boodschappenlst.clear();
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            Boodschappenlst);
    lstviewProducten.setAdapter(arrayAdapter);
    lstviewProducten.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Boodschappenlst.remove(position);
            arrayAdapter.notifyDataSetChanged();
        }
    });
}

1 个答案:

答案 0 :(得分:0)

        new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Are you sure?")
            .setMessage("do you want to delete this?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i("Button Tapped","Yes");
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i("Button Tapped","No");
                }
            })
            .show();