I have searched all the questions regarding this problem on Stack Overflow and I was not able to get the correct answer. The answers posted in the answer section or comment sections were not working for me. I have tried to log the values but addValueEventListener is not triggering. However, I can see the correct database reference (toString) just before the the addValueEventListener function in the logs. I have allowed all users to the database so there shouldn't be an authentication issue. I tried adding this to MainActivity.class as well but that doesn't seem to work. I have SHA1 fingerprint on the database and there is a connection to the database as well since my log.d prints the database reference child value.
Rules for my database are as follows:
{
"rules": {
".read": "auth == null",
".write": "auth == null",
}
}
ReadWriteActivity.class
package com.example.grocery;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
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;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.*;
public class ReadWriteActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_write);
listView = findViewById(R.id.listView);
final ArrayList<String> list = new ArrayList<>();
final ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
listView.setAdapter(adapter);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("names");
Log.d("DATABASE REFERENCE", reference.toString());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
list.clear();
for (DataSnapshot datasnapshot : snapshot.getChildren()) {
Log.d("KEY VALUE", datasnapshot.getValue().toString());
list.add(datasnapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w("KEY ERROR", "Failed to read value.", error.toException());
}
});
}
}
activity_read_write.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ReadWriteActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp"
android:textStyle="bold"/>