简体   繁体   中英

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);

Call cursor.notifyDataSetChanged(); which tells the list to repopulate with the new data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM