java - Google Firebase 方法 onDataChange() 未调用

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

    }

   }

最佳答案

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

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

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

关于java - Google Firebase 方法 onDataChange() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616437/

相关文章:

java - 在 Play 框架中将 Scala session 对象转换为 Java session 对象

java - 警报对话框中的首选项 Activity

android - 我正在使用 firebase 数据库,我只想知道我是否可以或如何检索两个子子项中的数据?

java - 如何从文件中读入并在 GUI 中的文本字段/标签中显示?

java - 使用tomcat在http请求之间确定优先级

java - 如何为具有泛型的接口(interface)创建对象

java - Android 解析无法将子类列表固定到缓存

android - 删除某个字段具有相同值的多行

ios - Firebase 上基于用户的私有(private)数据

ios - 数字到 firebase 迁移 iOS 电话号码输入