My application logs data about incoming phone calls and sms for particular dates and keeps them in a list. I want the application to check if there is already an entry for that date when a new phone call or sms comes in. If this is the case, I want the application to increment a value in the list.
However, when I try to do that I get this error: java.util.ConcurrentModificationException
How can I remedy this problem?
My codes looks like this
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
}