【问题标题】:Why my dismiss() method not able close Customize Alert dialog为什么我的 dismiss() 方法无法关闭自定义警报对话框
【发布时间】:2013-11-06 02:35:19
【问题描述】:

我正在开发一个 android 应用程序,我在其中创建了自定义警报对话框。我声明全局警报对话框和 AlertDialog.builder 如下。现在我在按钮点击中调用了三个方法f1()、f2()、f3()。

btn_my_order.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
      f1();
      f2(); 
      f3();
  return false;
        }
    });

我声明 orderDialog 并全局构建如下:-

 private AlertDialog orderDialog = null;
 AlertDialog.Builder builder;

我的 f1() 块如下:-

       F1{

        builder = new AlertDialog.Builder(MainScreen.this);
    mContext = getApplicationContext();
    /**
     * by the help of inflater my ordre is showing in list view
     */
    inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
    orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);

    ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
    tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
    tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
    tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
    Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
    Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);

    bclose.setOnClickListener(new OnClickListener() {

        public void onClick(View v) { 
            orderDialog.dismiss();   
            System.out.println(" click on close button");


        }      
    });

    /**      
     * click of place order to kitchen    
     */   
    bPlaceOrder.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            System.out.println("Place order click");

            palceMyOrdertoServer();
            new SendOrderFromTable().execute();
            System.out.println("place order to server is called");
            String msg = "Your Order is Successfully placed to Kitcken";
            Message msgObject = new Message();
            msgObject.what = 1;             
            msgObject.obj = msg;
            addMenuItemHandler.sendMessage(msgObject);
        orderDialog.dismiss();

        }
    });}

我的 f2() 用于一些与数据库一起使用的光标

    F2{
   // many stuff to be here populate data from cursor and bind it with adapter
    // no any issue in this mehod
     }

现在我终于调用 f3()

      F3{
        builder.setView(orderDialogLayout);
    orderDialog = builder.create();
    orderDialog.show();
       }

现在我要解释我所有的问题 f1() 方法用于初始化 f2() 用于填充数据,而 f3() 用于显示自定义警报对话框。为什么我的

       orderDialog.dismiss();

不适合我。即使我能够看到带有消息的 logcat

        "Click on close button"

这意味着执行正在dismiss() 方法上,那么为什么自定义警报对话框在单击时没有关闭。在此先感谢大家

【问题讨论】:

  • 这是因为您在 f3 中初始化 orderDialog 时在 f1 中将其关闭
  • @Raghunandan ..这意味着如果我在 f3() 中调用了dismiss,那么它可以工作吗?..感谢您的快速回复..
  • 试试看告诉我
  • @Raghunandan.. 我试过你的建议,但仍然遇到同样的问题.. 请帮我解决这个问题
  • 确定 orderDialog 已显示/?

标签: android android-alertdialog dismiss


【解决方案1】:

您应该在您的 orderDialog 私有变量中添加 final

【讨论】:

  • @muntssir ....感谢您的回复..如果我宣布为最终结果。那么我如何在 f3() 块中初始化 orderDialog...?
  • 尝试重新考虑您的方法。 Here 这就是为什么它应该是最终的
  • 我必须说你的编码风格读起来不太愉快。重新考虑可能是个好主意。
  • @muntasir..,,,..我正在检查您的链接..谢谢...我可以问一个问题,我的编码风格有什么不愉快的地方吗?
  • @BlueGreen 实现 DialogInterface.OnDismissListener 覆盖 onDismiss(DialogInterface dialog) 并在那里记录一些东西,看看它是否记录了任何东西。 alertDialog.setOnDismissListener(ActivityName.this)
猜你喜欢
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多