如何从Android中的firebase实时数据库中检索数据?

时间:2017-02-05 16:26:25

标签: android google-maps firebase firebase-realtime-database

我有一个firebase实时数据库和位置节点,其中包含纬度和经度。我想在Android应用程序创建时从数据库中检索所有位置并在其上放置标记。我使用下面的代码来实现这一点但是虽然调用了onDataChange方法,但我无法从firebase获取数据。任何帮助将不胜感激。

 @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    ButterKnife.bind(this);

    Bundle bundle = getIntent().getExtras();
    userId = bundle.getString("uid");
    _shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG2, "ONCLICKKK");
            mFirebaseInstance = FirebaseDatabase.getInstance();
            mFirebaseDatabase = mFirebaseInstance.getReference("locations");

            mFirebaseDatabase.child("locations").addListenerForSingleValueEvent(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // Get user value
                            Log.d(TAG, "ondatachange called");
                           // Marker mark = nGoogleMap.addMarker(new MarkerOptions().position(new LatLng(-33.852, 151.211)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

                            List<LocationData> locationData = new ArrayList<LocationData>();

                            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                locationData.add(postSnapshot.getValue(LocationData.class));
                            }
                            for(int i = 0; i < locationData.size(); i++){
                                LatLng latLng = new LatLng(locationData.get(i).getLatitude(),locationData.get(i).getLongtitude());
                                Marker marker = nGoogleMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
                                Toast.makeText(getApplicationContext(), "lat: " + locationData.get(i).getLatitude()
                                        + "long : " + locationData.get(i).getLongtitude(),Toast.LENGTH_LONG).show();

                            }

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            System.out.println(databaseError);
                            System.out.println("ERROR DATABASE");

                        }

                    });
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
         mapFragment.getMapAsync(this);

         checkLocationPermission();

        }




            }});

0 个答案:

没有答案