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;    

        }

// 只有当用户在 alertdialog 中选择 yes 时,我如何将 onOptionsItemSelected 的数据传递给列表

4

1 回答 1

0

请参阅问题以获取更多信息。它链接到另一个答案,该答案讨论了如何以及何时可以使用notifyDataSetChanged

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

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

于 2011-08-27T19:48:17.313 回答