我的应用程序记录特定日期的来电和短信数据,并将它们保存在列表中。当新的电话或短信进来时,我希望应用程序检查是否已经有该日期的条目。如果是这种情况,我希望应用程序增加列表中的值。
但是,当我尝试这样做时,出现此错误: java.util.ConcurrentModificationException
我该如何解决这个问题?
我的代码看起来像这样
public void addLog(String phonenumber, String type, long date, int incoming, int outgoing)
{
//Check if log exists or else create it.
Log newLog = new Log(phonenumber, type, date, incoming, outgoing);
//Iterates through logs
for (Log log : logs)
{
if (log.getPhonenumber() == phonenumber && log.getDate() == date && log.getType() == type)
{
updateLog(newLog, log.getId());
}
else
{
android.util.Log.i("Datamodel", "Adding log");
logs.add(newLog);
//add to database
}
}
}
public void updateLog(Log newLog, long id)
{
//check for outgoing or incoming
if (newLog.getIncoming() == 1)
{
for (Log log : logs)
{
if (log.getId() == id)
{
//Increments incoming
int incoming = log.getIncoming();
android.util.Log.i("Datamodel", "Updating incoming");
log.setIncoming(incoming++);
}
else
{
//Increments outgoing
int outgoing = log.getOutgoing();
android.util.Log.i("Datamodel", "Updating outgoing");
log.setOutgoing(outgoing++);
}
}
}
//Update the list
//Add to database
}