How to stop firebase from repeating my chats

Viewed 78

I'm developing a chat app which uses firebase as a chatting server. The problem that I encounter is that the chat is repeating it self, i.e as soon as I send a message the previous messages are also sent and then reinflated on the screen.

This is my code:

if (!textmassage.getText().toString().trim().isEmpty()) {
        String massage = textmassage.getText().toString().trim();
        Map < String, String > hm = new HashMap <>();
        hm.put( "message", massage );
        hm.put( "user", "teacher" );
        hm.put( "name", teacher_name );
        hm.put( "mimetype", "null" );
        hm.put( "type", "text" );
        hm.put( "time", String.valueOf( tsLong ) );
        
        if (currentdate == 0) {
            Toast.makeText( this, "Live NOT Started", Toast.LENGTH_SHORT ).show();
        }
        else {
            String id = myRef.push().getKey();
            assert id != null;
            myRef.child( cls_name + "_" + section_name + "_" + subject_name ).child( id ).setValue( hm );
            textmassage.setText( "" );
            textmassage.requestFocus( );
            scroll.fullScroll( ScrollView.FOCUS_DOWN );
            chat_data.clear( );
            Toast.makeText( this, "send", Toast.LENGTH_SHORT ).show( );
        }

myref is firebase database reference variable.

1 Answers

If you are binding your data in an adapter, try to clear that adapter or remove the data you have set in it and maybe it won't repeat it self

Or you could do what an above answer suggested and use the listener to your benefits.

Related