简体   繁体   English

如果用户在警报对话框中按下“是”按钮,如何在列表中添加项目,如果他按下“否”则不添加

[英]how do you add an item in a list if a user presses yes button in an alert dialog box and don't add it if he presses no

I am making an app in which if a user selects a submenu item I pop up an Alert dialog which asks his confirmation whether he wishes to save that item in his list and saves it if he presses yes and doesn't add it if he presses no.我正在制作一个应用程序,如果用户选择一个子菜单项,我会弹出一个警报对话框,询问他是否希望将该项目保存在他的列表中,如果他按下是则保存它,如果他按下则不添加它不。

You can use this to show alert:您可以使用它来显示警报:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure to do this?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    doSomeThing();
                                            dialog.cancel();
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

You add the element you get from the dialog to the ArrayList you use to feed your Adapter then feed the cursor = new yourAdapter(YourClass.this.getBaseContext(), android.R.layout.simple_list_item_1, dataList); You add the element you get from the dialog to the ArrayList you use to feed your Adapter then feed the cursor = new yourAdapter(YourClass.this.getBaseContext(), android.R.layout.simple_list_item_1, dataList);

Call cursor.notifyDataSetChanged();调用cursor.notifyDataSetChanged(); which tells the list to repopulate with the new data;它告诉列表重新填充新数据;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果显示对话框,当用户按返回按钮时,调用哪种方法? - Which method is called when user presses back button if a dialog is shown? 我正在寻找一个 switch 语句来根据用户按下的按钮添加不同的页面 - I am looking to make a switch statement to add a different page depending on which button the user presses 如何在警报对话框中添加.get(位置) - How to add .get(position) in alert dialog box 当用户在 Java 中按下按键时,如何启动应用程序? - How can you start an application when the user presses a keystroke in Java? 如何收听特定的按钮按下和主视图用户交互? - How do I listen for specific button presses, and home view user interactions? 当用户按下按钮时,如何在 Android 中创建一个名为“我的文件夹”的文件夹? - How do I create a Folder called “My Folder” in Android when the User presses a Button? 用户按下“后退”按钮时如何阻止应用重新启动 - How to stop app from restarting when user presses the back button 一旦用户按下下一个按钮,如何显示下一个文本行? - How to display the next text lines once user presses the next button? 用户按下“后退”按钮时如何停止“第二屏”音乐? - How to stop Second Screen music when user presses the back button? 如何拦截Android中耳机上的按键按下? - How do I intercept button presses on the headset in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM