我正在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中选择“是”时,如何将onOptionsItemSelected的数据传递给列表
答案 0 :(得分:0)
有关详细信息,请参阅this问题。它链接到另一个answer,其中讨论了如何以及何时使用notifyDataSetChanged。
我认为在你的AlertDialog中你需要一个“添加”按钮和一个“取消”按钮来取消将它添加到购物车。单击添加后,您可以将项目插入用户购物车。要向AlertDialog添加另一个按钮,您将使用ab.setNegativeButton(...),就像使用setPositiveButton一样。这只是我对它的看法,我是否正确地理解了你对onCreateDialog的使用 - 或者它只用于告诉用户它已被添加?
有关如何在AlertDialog中实施的更具体帮助,请参阅this问题。