2013-08-27 40 views
0

還是新到Android/java和有關包,消息和處理程序相當混亂(道歉,如果我的術語是不太正確的)。我有一個可以顯示多個對話框的自定義對話框類。監聽器被設置爲通過處理程序將數據傳回主要調用活動。的Android束和消息(有時多)

傳回的數據可能是一個或多個項目。在我的測試中,我試圖發回兩件物品。我嘗試了很多方法。我總是能夠成功地傳送單個項目並讓處理程序接收並提取它。做多個項目,當我失敗(方式略有不同。)

如果我把兩個項目進入包併發送一封郵件,處理程序似乎只接收第2包項目,不是第一次。

如果我把一個項目的捆綁,發送,清除束,然後把第二個項目在包併發送,似乎沒有任何被處理者可以接收活動掛起。

我也用msg.toString的()的輸出,注意,如果兩個messeages發送,「何時」第二的爲0。不知道這是否事項或沒有。

另外,我還試圖通過使用msg.sendToTarget以及handler.sendMessage(MSG)的消息傳遞,但它似乎並不重要,其使用。

代碼剪和輸出這裏:http://pastebin.com/xtUatEVu

我留在,但註釋掉了一些嘗試其他的東西。我真的不明白我做錯了什麼。

回答

0

經過一些大量的實驗後,我發現了一些東西。主要問題是一條消息只能使用一次,即它不是一個可重複使用的信封,您可以將新信件重新發送。此外,請確保捆綁包密鑰是唯一的。請注意,我也發現msg.what用於存儲dialogID。

的處理器

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"); 
         } 



        } 
       });