0
votes

I'm implementing a chat function in my app using firebase. I followed a tutorial to the letter and I'm not sure why the real-time database is not updating as the requests are being made. It says firebase is connected in the codebase. The implemented code is standard for requesting and pushing data to the realtime database. Would appreciate some insight on where the logic is failing in the sendMessage and addValueEventListener

package com.example.groupin;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class chatActivity extends AppCompatActivity {


    private DatabaseReference myDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        myDatabase = FirebaseDatabase.getInstance().getReference("Message");

        TextView myText = (TextView) findViewById(R.id.textView8);

        myDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                myText.setText(dataSnapshot.getValue().toString());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                myText.setText("CANCELLED");
            }
        });
    }

    public void sendMessage(View view){
        EditText myEditText = (EditText) findViewById(R.id.editTextTextPersonName);

        myDatabase.setValue(myEditText.getText().toString());
        myEditText.setText("");
    }
}
1
why this chat won't work what doesn't work ? what's it doing/not doing ? - a_local_nobody
What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? - Alex Mamo
@AlexMamo. I apologize for not being clear. When the chat activity is opened it's supposed to be able to send messages to the Firebase Realtime database and recieve them back. With everything being setup correctly I see that is in fact connected but when I run it, the messages are not being updated when I check the firebase console. Neither are they being updated in the app itself. the myText element is the main body of the chat function where you see messages being sent back and fourth and it is not being updated itself. - LEXERA.EXE
@a_local_nobody Please see the response I sent to Mr.Alex Mamo - LEXERA.EXE
I think the problem lies specifically in the addValueEventListener. It is not actually listening for any changes I'm just confused as to why that is. - LEXERA.EXE

1 Answers

0
votes

The problem came down to incorrect referencing of certain APIs. This was a blunder on my part. Firebase must be configured to reference the correct realtime-database at the correct location otherwise responses can not be triggered or received.