当前位置: 首页 > 知识库问答 >
问题:

未调用Firebase的ChildEventListener方法

范福
2023-03-14

我想在地图活动中添加标记,它的位置是从Firebase实时数据库中检索到的。

我尝试在onCreate()onMapReady()方法中实现AddChildEventListener(),并且当用户登录时(尽管没有错误,但它们都不起作用),我还使用logcat显示位置坐标,但它的值不显示。

    email = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("email", "");
    password = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("password", "");
    name = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("name", "");
    gender = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("gender", "");
    age = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("age", "");
    mobileNumber = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("mobileNumber", "");
    if (email == "" || password == "" || name == "" || mobileNumber == "" || age == "" || gender == "") {
        startActivity(new Intent(this, WelcomeActivity.class));
        return;
    }

    setContentView(R.layout.activity_maps);
    getSupportActionBar().setTitle(R.string.app_name);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseSignIn();

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedVideoAd.setRewardedVideoAdListener(this);
    rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    findNurseButton = findViewById(R.id.find_nurse);
    findNurseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            findNurseButton.setText("finding nearby nurses");

        }
    });

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    assert mapFragment != null;
    mapFragment.getMapAsync(this);

    getLocationPermission();
    createLocationRequest();
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            else {
                try {
                    for (Location location : locationResult.getLocations()) {
                        // Update UI with location data
                        // ...
                        sendNurseRequest();
                        lastLocation = location;
                        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));
                        Log.i("location updates: ", "latitude: " + location.getLatitude() + " ,longitude: " + location.getLongitude());
                        userReference.child("auth id").setValue(userID);
                        userReference.child("latitude").setValue(location.getLatitude());
                        userReference.child("longitude").setValue(location.getLongitude());
                    }
                }
                catch (NullPointerException e) {

                }
            }
        }
    };
    mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

    DatabaseReference nurseRef = FirebaseDatabase.getInstance().getReference().child("nurses available/2");
    nurseRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.e("database error", databaseError.getMessage());
        }
    });
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
   mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    if (locationPermissionGranted && checkInternet()) {
        getDeviceLocation();
    }
}

这是我的数据库

共有1个答案

毕泽宇
2023-03-14
DatabaseReference nurseRef = FirebaseDatabase.getInstance().getReference().child("nurses available/2"); // just remove the last child /2

虽然它是作为一个节点存在的,但我应该从引用中删除它,如果有人能解释它的话?

事先谢谢...

 类似资料:
  • FCM服务: 应用程序分级: 舱单: -我从FCM控制台发送消息,但没有到达。FCM控制台在消息发送时显示OK。 -我从java服务器发送消息,但消息没有到达。当消息发送时,Java服务器显示我OK。 -发送消息,我使用生成的令牌在Android设备。 -之前,我在导入FCM库时犯了一个错误,那个错误解决了。当每次发送消息时发生过去的错误时,Android上就会发生异常。现在什么也没收到。 -我在

  • 我正在尝试设置OTP验证,所以当用户输入他们的电话号码时,我会向他们发送pin码,调用,我会收到代码pin,但问题是当调用时,我想转到另一个活动,用户可以输入代码pin进行验证,但根本不会调用,我不明白为什么。任何帮助都将不胜感激,谢谢。

  • 阅读文档后发现,当路径不存在时,childEventListener似乎不会激发。 这是一个问题,因为我想向用户显示一条没有数据的消息。 我可以添加一个valueEventListener,就像这个答案一样,但我将查询限制为最新的值,即query.limitTolast()并且valueEventListener不限制ToLast而是获取路径中的所有数据。

  • 我有一个里面有片段和IntentService的活动。当我单击按钮对话框片段打开,我输入我的数据。之后,我单击确定,它调用getActive()。启动服务(意图)。服务通过本地广播管理器调用sendBroadcast(意图),但没有调用onAccess()方法。怎么了?这是我的广播接收器 我在这里登记 我的记忆法 还有我在服务中发送广播的方法