Android,从firebase数据库获取价值

时间:2017-06-08 05:20:00

标签: android firebase firebase-realtime-database location

我需要在地图上添加所有用户的标记。我正在保存我的firebase中的所有坐标

我的数据库看起来像:

enter image description here

所以,当我想从数据库中获取值:lat,lng(我在onMapReady方法中执行):

int allUsers = 100;
    while(allUsers >= 0) {
        double latAll = Double.parseDouble(users.child("lat").toString());
        double lngAll = Double.parseDouble(users.child("lng").toString());
        mMap.addMarker(new MarkerOptions().position(new LatLng(latAll, lngAll)));
    }

我收到错误

我在这里将值保存到我的数据库中:

GPSTracker gpstracker = new GPSTracker(mContext, SearchActivity.this);
                double locationLng = gpstracker.getLongitude();
                double locationLat = gpstracker.getLatitude();

                databaseReference.child("lng").setValue(locationLng);
                databaseReference.child("lat").setValue(locationLat);

这是我的错误:

  

致命的例外:主要                                                    处理:kz.mul.livemeet_finalversion,PID:24598                                                    java.lang.NumberFormatException:对于输入字符串:“https://livemeetfinalversion.firebaseio.com/ekeLu1W_1RU%3AAPA91bFt2knzJYknyWLpuFb1Wm1nxl0TkM-bWdkNrjaoPUmv5JP_hm6b64Hx3m3X805A3QR8paMAOABJN6DZcb99z_EfwGwadN1dUN6ngtqTbGwxD6iMLlP7IW6RvEzg9UTW7HQKHyT0/lat”                                                        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)                                                        在java.lang.Double.parseDouble(Double.java:547)                                                        at kz.mul.livemeet_finalversion.MapsActivity.onMapReady(MapsActivity.java:118)                                                        在com.google.android.gms.maps.SupportMapFragment $ zza $ 1.zza(未知来源)                                                        在com.google.android.gms.maps.internal.zzt $ zza.onTransact(未知来源)                                                        在android.os.Binder.transact(Binder.java:499)                                                        在com.google.android.gms.maps.internal.bw.a(:com.google.android.gms.DynamiteModulesB:82)                                                        在com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.DynamiteModulesB:1805)                                                        在android.os.Handler.handleCallback(Handler.java:751)                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                        在android.os.Looper.loop(Looper.java:154)                                                        在android.app.ActivityThread.main(ActivityThread.java:6119)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:1)

步骤1)创建一个PoJo.class,其中包含数据库中的所有键,例如age,lat,lng等。

步骤2)现在调用此方法获取所有数据。如下所示

    public void getAllUsersFromFirebase() { 
    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child("Parent_child");
    UserRef.keepSynced(true);
    UserRef.addValueEventListener(new ValueEventListener() {
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
            List<PoJo> fcmUsers = new ArrayList<>();
            while (dataSnapshots.hasNext()) {
                DataSnapshot dataSnapshotChild = dataSnapshots.next();
                PoJo fcmUser = dataSnapshotChild.getValue(PoJo.class);
                fcmUsers.add(fcmUser);

            } 

            // Check your arraylist size and pass to list view like 
            if(fcmUsers.size()>0)
            { 
                fcmUsers arraylist pass to your Map activity.

            }else 
            { 
                // Display dialog for there is no user available. 
            } 

        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) {
                // for handling database error           
        } 
    }); 
}