1
Query ref = mDatabaseReference.child("Messages")
            .child(MessageRecieverId).child(MessageSenderId).orderByChild("Seen").equalTo(false);
    ref.addValueEventListener(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);

                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!
        }
    });

The problem with this is the first time i open the activity and if there is one child to be added to the table in sqlite it adds normally without any problem and then i close the activity and i open it again and it adds the child twice even if there is just one and if i close and open it again it adds thrice instead of one and it just goes on... So why is this behavious and how can i change it to executing just once like the first time i open the activity... And i cannot use valueforsingleevent as i need to listen to listener when im inside the activity every second... and the removelistener too didnt work while closing the activity... So someone please help me out

  • Check also **[this](https://stackoverflow.com/questions/48861350/should-i-actually-remove-the-valueeventlistener/48862873)** out. – Alex Mamo Nov 22 '18 at 09:48

1 Answers1

1

It's probable that you never removed the listener when the activity finished. You add a listener with ref.addValueEventListener(), but never remove it with ref.removeEventListener(). If you never remove a listener, it will continue to trigger with new changes. On Android, you typically make those call symmetrical, so if you add a listener during onStart, you would remove it during onStop.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441