未调用Google Firebase方法onDataChange()

时间:2018-10-02 21:09:54

标签: java android firebase firebase-realtime-database

从Google Firebase读取数据时遇到问题。似乎无论我是否更改数据库中的数据,都不知道为什么不调用onDataChange方法。使我感到好奇的是,我能够将数据写入数据库。如果有人可以帮助我,我将非常感激。这是我的代码:

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) {

    }

   }

1 个答案:

答案 0 :(得分:1)

我刚刚发现错误,我删除了行

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

,现在将调用onDataChange方法。非常感谢您的帮助!