在 Android 中,如果用户按“是”,如何获得警告对话框以将项目添加到列表中,反之亦然

发布于 2024-12-01 20:56:30 字数 1736 浏览 0 评论 0原文

我正在 Android 中开发一个简单的餐厅应用程序,我使用了菜单和与每个菜单项相对应的膨胀子菜单项,我想知道一种仅当用户在警报对话框中选择“是”时才将菜肴名称添加到列表中的方法反之亦然。 这是我的代码

   public boolean onOptionsItemSelected(MenuItem m)
    {
        super.onOptionsItemSelected(m);
        switch(m.getItemId())
        {
            case R.id.Chicken_Biryani:
            selectedItem.add(m.getTitle().toString());
            cost.add(150);
            Toast.makeText(getApplicationContext(),"Hyderabadi special: Chicken   biryani costs 150 Rs"+selectedItem, Toast.LENGTH_LONG).show();
            showDialog(ALERT_DIALOG); 
            break;

            case R.id.Butter_Chicken:
            selectedItem.add(m.getTitle().toString());
            cost.add(150);
            Toast.makeText(getApplicationContext(),"Now with Punjabi Tadka: Butter Chicken costs 150 Rs", Toast.LENGTH_LONG).show();
            showDialog(ALERT_DIALOG);   

            break;

                   ........

      public Dialog onCreateDialog(int id) {
        switch(id)
        {
        case ALERT_DIALOG:
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Buy Items");
        ab.setMessage(" You have added the item to your cart ");
        ab.setIcon(R.drawable.shopcart);
        ab.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){         
            Toast.makeText(getApplicationContext(),"Item added to cart your cart contains "+selectedItem.size()+" Items", Toast.LENGTH_LONG).show();    
            }});    
    Dialog ad = ab.create();
        return ad;  
        }
        return null;    

        }

//仅当用户在alertdiaolog中选择yes时,我如何将onOptionsItemSelected的数据传递到列表

I am developing a simple Restaurant application in Android, I used menus and inflated submenu items corresponding to each menu item, I wanted to know a way of how to add the name of the dish to list only if the user selects yes in the alert dialog and viceversa.
Here is my code

   public boolean onOptionsItemSelected(MenuItem m)
    {
        super.onOptionsItemSelected(m);
        switch(m.getItemId())
        {
            case R.id.Chicken_Biryani:
            selectedItem.add(m.getTitle().toString());
            cost.add(150);
            Toast.makeText(getApplicationContext(),"Hyderabadi special: Chicken   biryani costs 150 Rs"+selectedItem, Toast.LENGTH_LONG).show();
            showDialog(ALERT_DIALOG); 
            break;

            case R.id.Butter_Chicken:
            selectedItem.add(m.getTitle().toString());
            cost.add(150);
            Toast.makeText(getApplicationContext(),"Now with Punjabi Tadka: Butter Chicken costs 150 Rs", Toast.LENGTH_LONG).show();
            showDialog(ALERT_DIALOG);   

            break;

                   ........

      public Dialog onCreateDialog(int id) {
        switch(id)
        {
        case ALERT_DIALOG:
        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setTitle("Buy Items");
        ab.setMessage(" You have added the item to your cart ");
        ab.setIcon(R.drawable.shopcart);
        ab.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){         
            Toast.makeText(getApplicationContext(),"Item added to cart your cart contains "+selectedItem.size()+" Items", Toast.LENGTH_LONG).show();    
            }});    
    Dialog ad = ab.create();
        return ad;  
        }
        return null;    

        }

// how do i pass the data of the onOptionsItemSelected to the list only if the user selects yes in the alertdiaolog

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-12-08 20:56:30

请参阅问题了解更多信息。它链接到另一个答案,其中讨论了如何以及何时可以使用通知数据集更改

我认为在您的 AlertDialog 中您需要一个“添加”按钮和一个“取消”按钮来取消将其添加到购物车。单击“添加”后,您会将商品插入用户购物车。要向 AlertDialog 添加另一个按钮,您可以使用 ab.setNegativeButton(...) ,就像使用 setPositiveButton 一样。这只是我的看法,我是否正确理解了您对 onCreateDialog 的使用 - 或者它仅用于告诉用户它已被添加?

请参阅问题,获取有关如何在 AlertDialog 中实施的更具体帮助。

Please see this question for more information. It links to another answer which talks about how and when you can use notifyDataSetChanged.

I think in your AlertDialog you would want an "Add" button and a "Cancel" button to cancel adding it to the cart. Upon clicking add, your would insert the item into the users cart. To add another button to your AlertDialog you would use ab.setNegativeButton(...) just as you did with the setPositiveButton. This is just my take on it, am I correctly understanding your use of onCreateDialog - or is it used only to tell the user it has been added?

Please see this question for more specific help on how to implement within an AlertDialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文