0

对 android/java 来说还是新手,并且对包、消息传递和处理程序非常困惑(如果我的术语不太正确,请道歉)。我有一个可以显示多个对话框的自定义对话框类。侦听器被设置为通过处理程序将数据传递回主调用活动。

传回的数据可能是一项或多项。在我的测试中,我试图寄回两件物品。我已经尝试了很多方法。我总是成功地传输单个项目并让处理程序接收并提取它。做多个项目时我失败了(方式略有不同。)

如果我将两个项目放入包中并只发送一条消息,则处理程序似乎只接收第二个包项目,而不是第一个。

如果我将一个项目放入捆绑包中,发送,清除捆绑包,然后将第二个项目放入捆绑包中并发送,处理程序似乎没有收到任何内容并且活动挂起。

我还使用了 msg.toString() 的输出,并注意如果发送了两条消息,则第二条的“何时”为 0。不知道这是否重要。

此外,我尝试使用 msg.sendToTarget 和 handler.sendMessage(msg) 传递消息,但使用哪个似乎并不重要。

代码片段和输出在这里: http: //pastebin.com/xtUatEVu

我已经离开了,但注释掉了其他一些尝试过的东西。我真的不明白我做错了什么。

4

1 回答 1

0

好吧,经过一些广泛的实验,我发现了一些东西。主要问题是一条消息只能使用一次,即它不是一个可重复使用的信封,您可以放入新的信件并再次发送。此外,请确保捆绑密钥是唯一的。请注意,我还发现了用于存储 dialogID 的 msg.what。

处理程序

private Handler uiMsgHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.i(TAG, "%%%% in the uiMsgHandler");

        // msg.what() contains the dialogID.  The bundle key is the itemID
        if (msg != null) {
            int DlgId = 0;
            int ItemId = 0;

            String value = "";  //leave it to each case to recast the value
            String element = "";

            Bundle b = new Bundle();
            Set<String> s = null;

            b = msg.getData();
            Log.i(TAG,"bundle is "+b);

            DlgId = msg.what;
            Log.i(TAG,"DigId is "+DlgId);

            switch (DlgId) {
                case 1:

                    s = b.keySet(); //find the set of keys in this message bundle
                    Log.i(TAG,"s is "+s.toString());
                    Log.i(TAG,"s.size is "+s.size());

                        if (s.size() < 100) {  // we allow up to 100 items, 99 is reserved

                            Iterator itr = s.iterator();
                            while (itr.hasNext()) {
                                element = (String) itr.next();
                                ItemId = Integer.parseInt(element.substring(0,1));
                                value = b.getString(element);
                                Log.i(TAG,"ItemID is "+ItemId+" value is "+value+" itr.next "+itr.hasNext());

                                // now we can handle each specific item

                                switch (ItemId) {
                                    case 1:
                                        Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
                                        // do something
                                        // close the dialog
                                        break;
                                    case 2:
                                        Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
                                        // do something
                                        // close the dialog
                                        break;
                                    case 99:
                                        Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+" is "+value);
                                        // Cancel button was pressed
                                        // close the dialog
                                        break;
                                    default: /* item out of range */
                                        Log.i(TAG,"Error ItemID OoR: DialogID "+DlgId+" with message item id "+ItemId+" is out of range");
                                        // close the dialog and toast w/ message
                                        break;
                                } // end of ItemID switch

                            } // end of ItemID iterator while

                        } else { // end of number of items size check
                            Log.i(TAG,"too many items, must be < 100 but is "+s.size());
                        }
                break; // end of case 1

                default: /* dialog id was out of range */
                         Log.i(TAG,"Error: dialog id was out of range");
                         Log.i(TAG,"Bundle data for Dialog "+DlgId+" Item "+ItemId+"is "+value.substring(2));
                        // close the dialog
                        // toast with a message
                break;

            } // end of Dialog switch

        } // msg null check


     }
};

==================================================== ====================

自定义对话框的一部分

                acceptButton = (Button) dialogview.findViewById(r_id_accept);
                rejectButton = (Button) dialogview.findViewById(r_id_reject);

                acceptButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Inform the user the button has been clicked
                        Log.i(TAG, "ACCEPT BUTTON PRESSED");

                        // now we are going to do the messaging
                        // addOptQty is item id 01
                        // addOptPrice is item id 02
                        // msg.what contains the dialogID

                        msg.what=id;
                        b.putString("1",""+addOptQty);
                        b.putString("2",""+addOptPrice);
                        msg.setData(b);

                        // now send the message to the handler
                        try {
                            mResponseHandler.sendMessageDelayed(msg,10);
                        } catch (Exception e) {
                            Log.i(TAG, "ERROR SENDING MESSAGE");
                        }



                    }
                });

                rejectButton.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Inform the user the button has been clicked
                        Log.i(TAG,"LOCI BUTTON PRESSED");

                        msg.what=id;
                        b.putString("99","CANCEL");
                        msg.setData(b);

                        // now send the message to the handler
                        try {
                            mResponseHandler.sendMessageDelayed(msg,10);
                        } catch (Exception e) {
                            Log.i(TAG, "ERROR SENDING MESSAGE");
                        }



                    }
                });
于 2013-08-29T19:59:58.110 回答