仍然是android / java的新手,对包,消息和处理程序非常困惑(如果我的术语不太正确,请道歉)。我有一个自定义对话框类,可以显示多个对话框。监听器设置为通过处理程序将数据传递回主调用活动。
传回的数据可能是一项或多项。在我的测试中,我试图发回两件物品。我已经尝试了很多方法。我总是成功地传输单个项目并让处理程序接收并提取它。我做多件事时会失败(方式略有不同。)
如果我将两个项目放入捆绑包并仅发送一条消息,则处理程序似乎只接收第二个捆绑项目,而不是第一个。
如果我在捆绑包中放入一个项目,发送,清除捆绑包然后将第二个项目放入捆绑包中并发送,处理程序似乎没有收到任何内容并且活动挂起。
我还使用了msg.toString()的输出,并注意如果发送了两个messeage,则第二个的“when”为0.不知道是否重要。
另外,我尝试使用msg.sendToTarget以及handler.sendMessage(msg)传递消息,但似乎并不重要。
代码剪切并在此处输出:http://pastebin.com/xtUatEVu
我已离开,但评论了其他一些尝试过的事情。我真的不明白我做错了什么。
答案 0 :(得分: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");
}
}
});