保存模型类中的数据或Firebase中的列表

时间:2017-05-26 06:52:09

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

我想获取所有设备的纬度和经度以及数据,例如643004687464300468756430046876

enter image description here

 new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


    FirebaseDatabase database = FirebaseDatabase.getInstance();

    myRef = database.getReference("GPSLocationHistory/DevicesLatest/6430046874");


    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, Object> objectHashMap = dataSnapshot.getValue(objectsGTypeInd);
            ArrayList<Object> objectArrayList = new ArrayList<Object>(objectHashMap.values());
            System.out.println("value 0= " + objectArrayList.get(0) + " value 1= " + objectArrayList.get(1) + "value 2= " + objectArrayList.get(2) + " value 3= " + objectArrayList.get(3) + "value 4= " + objectArrayList.get(4) + " value 5= " + objectArrayList.get(5) + " value 6= " + objectArrayList.get(6));
            // LatLng location = new LatLng(latitude, longitude);
            String latLng = objectArrayList.get(2).toString() + "," + objectArrayList.get(3).toString();
            System.out.print("oyy " + latLng);
            Toast.makeText(MapsActivity.this,""+latLng.toString(),Toast.LENGTH_SHORT).show();

             latlongFcm = new LatLng(Double.parseDouble(objectArrayList.get(2).toString()), Double.parseDouble(objectArrayList.get(3).toString()));
           mMap.clear();
            Marker marker;
             marker = mMap.addMarker(new MarkerOptions()
                    .position(latlongFcm)
                    .title("San Francisco")
                    .snippet("Population: 776733"));


            for (DataSnapshot alert : dataSnapshot.getChildren()) {
                System.out.println(alert.getValue());
            }


        }

此代码仅显示一个用户的数据,如6430046874

我想创建Model类并从这里获取所有用户的数据并绘制标记。

2 个答案:

答案 0 :(得分:3)

步骤1)创建一个PoJo.class,其中包含具有相似数据类型的DeviceLatest子节点中的所有键

步骤2)在您的场景中转到所需的子节点,它是GpsLocationHistory-DeviceLatest。所以这里我们要花两个孩子的时间..请更正子节点的拼写

步骤3)记下以下方法并致电。

public void getAllUsersFromFirebase() {
    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child("GpsLocationHistory").child("DeviceLatest");
    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 list view
            }else
            {
                // Display dialog for there is no user available.
            }

        }

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

答案 1 :(得分:0)

你只得到一个,因为你没有迭代整个对象。所以为了解决这个问题,请使用以下代码:

FirebaseDatabase database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("GPSLocationHistory").child("DevicesLatest");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String deviceId = (String) ds.getKey();

            DatabaseReference deviceIdRef = database.child("GPSLocationHistory").child("DevicesLatest").child(deviceId);
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Double lati = (Double) dataSnapshot.child(lati).getValue();
                    Double longi = (Double) dataSnapshot.child(longi).getValue();
                    //and so on for the other values
                    //and do what you want with this values
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException(); // don't ignore errors
                }
            };
            deviceIdRef.addListenerForSingleValueEvent(eventListener);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
};
myRef.addListenerForSingleValueEvent(eventListener);