huhn3k
huhn3k

Reputation: 91

Google Firebase method onDataChange() is not called

I've got a problem with reading data from google firebase. It seems like the method onDataChange is not called no matter if I change data in the database or not and I don't know why. What makes me curious is that I am able to write data into the database. I would be very thankful if someone could help me. Here is my code:

public class MainActivity extends AppCompatActivity implements LocationListener, View.OnClickListener {

    MapView mapView;
    MyLocationNewOverlay myLocationOverlay;
    private static final String TAG = "MainActivity";

    String content = "a";
    String author = "b";
    Double latitude = 53.2;
    Double longitude = 11.5;
    private ItemizedIconOverlay<OverlayItem> messagesOverlay;
    private List<OverlayItem> items = new ArrayList<>();
    private long totalNumberChilds;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //set up the mapView and show the MyLocationOverlay
        mapView = findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        mapView.getController().setZoom(5);
        messagesOverlay = new ItemizedIconOverlay<OverlayItem>(items, getResources().getDrawable(R.drawable.briefumschlag), null, this);
        myLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(getApplicationContext()), mapView);
        myLocationOverlay.enableMyLocation();
        mapView.getOverlays().add(messagesOverlay);
        mapView.getOverlays().add(myLocationOverlay);
        GeoPoint point = this.myLocationOverlay.getMyLocation();
        if(point==null){
            return;
        } else {
            mapView.getController().animateTo(point);
        }

        //initialize the Overlay for the Messages


        //messagesOverlay.addItem(new OverlayItem(author, "", new GeoPoint(latitude, longitude)));

        //declare the database variables in onCreate
        //NOTE: Unless you are signed in, this will not be useable.
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");

        // Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                messagesOverlay.removeAllItems();
                Toast.makeText(MainActivity.this, "yesss", Toast.LENGTH_LONG).show();
                Log.d(TAG, "onDataChange: Successsfully called!");
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    //Message message = new Message();
                    //String author1 = ds.child("messageAuthor").getValue(String.class);
                    //String content1 = ds.child("messageContent").getValue(String.class);
                    //Double latitude1 = ds.child("messageLatitude").getValue(Double.class);
                    //Double longitude1 = ds.child("messageLongitude").getValue(Double.class);
                    //messagesOverlay.addItem(new OverlayItem("by" + author1, "", new GeoPoint(latitude1, longitude1)));

                }

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.testButton) {
            addMessage();
        }
    }

    public void addMessage() {
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");

        String id = myRef.push().getKey();

        Message message = new Message(author, content, latitude, longitude);

        myRef.child(id).setValue(message);

        Toast.makeText(this, "Message added", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onLocationChanged(Location location) {
        GeoPoint point = new GeoPoint(location);
        if(point==null){
            return;
        } else {
            mapView.getController().animateTo(point);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

   }

Upvotes: 0

Views: 139

Answers (1)

huhn3k
huhn3k

Reputation: 91

I just found the error, I removed the lines

if(point==null){ 
return;
 } else { 
mapView.getController().animateTo(point); 
}

and now the onDataChange method is called. Thank you so much for your help!

Upvotes: 1

Related Questions