Java 未调用Google Firebase方法onDataChange()
我在从google firebase读取数据时遇到问题。无论我是否更改数据库中的数据,似乎都不会调用onDataChange方法,我不知道为什么。让我好奇的是,我能够将数据写入数据库。如果有人能帮助我,我将非常感激。这是我的密码:Java 未调用Google Firebase方法onDataChange(),java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我在从google firebase读取数据时遇到问题。无论我是否更改数据库中的数据,似乎都不会调用onDataChange方法,我不知道为什么。让我好奇的是,我能够将数据写入数据库。如果有人能帮助我,我将非常感激。这是我的密码: public class MainActivity extends AppCompatActivity implements LocationListener, View.OnClickListener { MapView mapView; MyLo
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) {
}
}
public类MainActivity扩展AppCompatActivity实现LocationListener、View.OnClickListener{
地图视图;
MyLocation新建覆盖MyLocation覆盖;
私有静态最终字符串TAG=“MainActivity”;
字符串content=“a”;
字符串author=“b”;
双纬度=53.2;
双经度=11.5;
私人项目法令发布消息;
私有列表项=新的ArrayList();
私人长文档;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置地图视图并显示MyLocationOverlay
mapView=findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
设置多点触控控件(真);
mapView.getController().setZoom(5);
messagesOverlay=newitemizedictionoverlay(items,getResources().getDrawable(R.drawable.briefleumschlag),null,this);
myLocationOverlay=新建MyLocationNewOverlay(新建GpsMyLocationProvider(getApplicationContext()),mapView);
myLocationOverlay.enableMyLocation();
mapView.getOverlays().add(messagesOverlay);
mapView.getOverlays().add(myLocationOverlay);
地理点=this.myLocationOverlay.getMyLocation();
如果(点==null){
返回;
}否则{
mapView.getController().animateTo(点);
}
//初始化消息的覆盖
//messagesOverlay.addItem(新叠加项(作者“”,新地理点(纬度、经度));
//在onCreate中声明数据库变量
//注意:除非您已登录,否则此选项将不可用。
FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference(“消息”);
//从数据库中读取
myRef.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//使用初始值调用此方法一次,然后再次调用
//每当更新此位置的数据时。
messagesOverlay.removeAllItems();
Toast.makeText(MainActivity.this,“yesss”,Toast.LENGTH\u LONG.show();
d(标记为“onDataChange:Successsfully called!”);
对于(DataSnapshot ds:DataSnapshot.getChildren()){
//消息消息=新消息();
//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(新叠加项(“由”+author1“”,新地质点(纬度1,经度1));
}
}
@凌驾
已取消的公共无效(DatabaseError错误){
//无法读取值
}
});
}
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.testButton){
addMessage();
}
}
public void addMessage(){
FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference(“消息”);
字符串id=myRef.push().getKey();
消息消息=新消息(作者、内容、纬度、经度);
myRef.child(id).setValue(消息);
Toast.makeText(此“消息已添加”,Toast.LENGTH_LONG).show();
}
@凌驾
已更改位置上的公共无效(位置){
地质点=新的地质点(位置);
如果(点==null){
返回;
}否则{
mapView.getController().animateTo(点);
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
}
我刚发现错误,我删除了行
if(point==null){
return;
} else {
mapView.getController().animateTo(point);
}
现在调用onDataChange方法。非常感谢你的帮助 你能从firebase数据库中发布规则吗?@Skizo ozᴉʞ是我读写过的true@Barns谢谢你的回答,但是我试过了,但仍然没有发生任何事情。第一个故障排除步骤是不要忽略错误。最简单有用的
onCancelled
实现是:@Override public void onCancelled(DatabaseError error){throw error.toException();}
这行代码是否有效Log.d(标记“onDataChange:successfully called!”)代码>?