sqlite = mDatabaseReference.child("Messages")
.child(MessageSenderId).child(MessageRecieverId).orderByChild("Opened").equalTo(0);
valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long count = 0;
for (DataSnapshot ds : dataSnapshot.getChildren()) {
count = dataSnapshot.getChildrenCount();
MainData helper = new MainData(getApplicationContext()); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
String newId = "MyData";
Cursor data = helper.getData();
long newDatar = 0;
long newDatat = 0;
while (data.moveToNext()) {
newId = data.getString(data.getColumnIndex("Data"));
newDatar = data.getInt(data.getColumnIndex("TotalMessagesRecieved"));
newDatat = data.getInt(data.getColumnIndex("TotalMessages"));
}
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_DATA, newId);
contentValues.put(KEY_TOTAL_MESSAGES_RECIEVED, (newDatar + count));
contentValues.put(KEY_TOTAL_MESSAGES, (newDatat + count));//Change the value of newData(which is actually your old value) by incrementing
long returnVariable = db.update(TABLE_MAIN_DATA, contentValues, null, null);
Log.d("FUCKU", String.valueOf(count) + "/" + String.valueOf(newDatar) + "/" + String.valueOf(newDatat));
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(), "r", Toast.LENGTH_SHORT).show();
}
}
Log.d("CMONNN", String.valueOf(count)); //Will print the number of seen messages
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("CMONNN", databaseError.getMessage()); //Don't ignore errors!
}
};
sqlite.addListenerForSingleValueEvent(valueEventListener);
您可以看到它执行了多次,但不是随机执行,而是执行的次数等于count的值。如果count为1,则执行一次。如果为2,则执行两次,如果三次,则执行三次。但是,即使它是单个值事件侦听器,为什么也会发生这种情况?