如果我理解得很好,你会这样做:
- 对于地址簿中的每个联系人,请执行以下操作:
- 继续检查邮件,直到我正在检查的联系人回复
还没到。 (按 1 到 1 的顺序检查回复)
但你想反过来:
- 继续检查邮件直到收到回复:
- 检查谁发送了回复并更新表格。
您可以通过一个简单的 while 循环和一个查找图来实现这一点:
Map<String, Integer> replies = new HashMap<String, Integer>();
for (int j = 0; j < v.size(); j++)
replies.put(((Member)v.getElementAt(j)).getPhoneNo(), j); //Save a reference to the phone number AND its index in "v".
MailReader mR2 = new MailReader();
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "********@gmail.com";
String password = "********";
while(!replies.isEmpty()){
MailReader.check(host, mailStoreType, username, password); //Is this correct? Why are you using a static method? What's the purpose of mR2?
String phoneToCheck = MailReader.getPhone(); //You have to implement this if it's not present
if (replies.containsKey(phoneToCheck)) {
newFrame.addNotify();
System.out.println("IT WORKED");
model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
model.fireTableDataChanged();
replies.remove(phoneToCheck);
}
}
编辑
如果您的 v 对象与您的表结构不同,您可以直接从表中获取电话号码(假设已填满):
for(int i = 0; i < model.getRowCount(); i++){
replies.put((String)model.getValueAt(i,3), i); //Assuming 3 is phone number column
}
编辑 2
也许我的解释不够好。
在您的修改中,您根据需要更改了 Map 结构,但您使其无用。
Map 是用于保存键值对的结构。在您的情况下(我的建议)用于保存字符串和整数。字符串代表电话号码,而整数代表其在表格中的位置。因此,如果我们有这样的表:
------------+---------------
| 1 | 1234567 |
------------+---------------
| 2 | 7654321 |
------------+---------------
我们想要一个包含值的地图:
Map = {[12345678, 1],[7654321, 2]}
这样,当我们调用Map.get("7654321")("7654321" 是键)时,我们将得到2,这是表格中电话号码的行。
现在你的代码:
Map<String, Integer> replies = new HashMap<String, Integer>();
//First thing first, fill the map with the phone number and row number
//as described above.
for (int j = 0; j < v.size(); j++)
replies.put(((Member)v.elementAt(j)).getPhoneNo(), j);
// ^ ^
// phone number row
//[omissed for brevity]
//This is wrong, we want to check if the map has still phone numbers in it
//while(phoneCheck >0){
while(!replies.isEmpty()){ // This means "while replies is not(!) empty"
//I don't know how MailReader is implemented, but I assume that this call
//checks if a new mail has arrived.
MailReader.check(host, mailStoreType, username, password);
//Now comes the IMPORTANT part. You have to retrieve the phone number
//from the INCOMING MAIL! So you can understand who replied.
//Again, I don't know how MailReader is implemented, I assumed that a getPhone()
//method is available.
//Is such method doesn't exist you have to CREATE IT. It must retrieve the
//phone number contained in the mail.
String phoneToCheck = MailReader.getPhone(); //Get the phone from the mail.
//This is completely useless.
/**
for(int i = 0; i<v.size(); i++) {
con = (Member) v.elementAt(i);
phoneToCheck[i] = con.getPhoneNo();
**/
if (replies.containsKey(phoneToCheck)) {
newFrame.addNotify();
System.out.println("IT WORKED");
//Where the hell does i came from?? It belongs to the for loop you FINISHED earlier!!
//model.setValueAt(MailReader.getReply(phoneToCheck[i]), replies.get(phoneToCheck[i]), 3);
model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
model.fireTableDataChanged();
replies.remove(phoneToCheck);
//phoneCheck --; Useless
}
}
}
}
我可以为您找到使用数组的唯一含义是邮件一次包含多个电话号码。在这种情况下,您必须以这种方式修改代码:
String phonesToCheck[] = MailReader.getPhones(); //Get the phones from the mail. BEWARE!! This is an array!
for(String phoneToCheck: phonesToCheck){
if (replies.containsKey(phoneToCheck)) {
newFrame.addNotify();
System.out.println("IT WORKED");
model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
model.fireTableDataChanged();
replies.remove(phoneToCheck);
}
}