【发布时间】:2018-05-03 21:44:23
【问题描述】:
在 Firebase 实时数据库中,我为每个列表项创建了一个具有唯一键的列表。
String deviceId = dbDevice.push().getKey();
dbDevice.child(deviceId).setValue(device);
在另一个 Activity 中,我想按键循环遍历每个设备,并为每个特定设备调用方法 updateLatestDistance。
虽然下面代码的结果以一种奇怪的方式运行。 例如。我有 2 个列表项,所以我将有两个距离属性,每个列表项一个。在调用 updateLatestDistance 方法时,应该更新特定列表项中的距离值,尽管这会导致两个列表项都包含 2 个最新距离。
Database dbDev= FirebaseDatabase.getInstance().getReference("Device");
dbDev.addValueEventListener(new ValueEventListener() {
//In the onstart method
@Override
public void onDataChange(DataSnapshot dataSnapshot) { //Read the value
//Clear and regenerate the list so that no item is multipled.
devices.clear();
//Loop
for(DataSnapshot deviceSnapshot : dataSnapshot.getChildren()){
Device device = deviceSnapshot.getValue(Device.class);
//Add the fetched data into the local list.
devices.add(device);
String checkId = deviceSnapshot.getKey();
for(int i =0; i<devices.size(); i++){
//Grab the latest safety_zone's latitude for later usage.
DatabaseReference dbSafe_La = FirebaseDatabase.getInstance().getReference("Device/" +checkId+ "/SafetyZone/s_latitude");
dbSafe_La.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Check if safetyZone exists.
//If no, a 0.0 is passed.
if(dataSnapshot.exists()){
la = dataSnapshot.getValue(double.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Grab the latest safety_zone's longtitude for later usage.
DatabaseReference dbSafe_Lo = FirebaseDatabase.getInstance().getReference("Device/" +checkId+ "/SafetyZone/s_longitude");
dbSafe_Lo.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Check if safetyZone exists.
//If no, a 0.0 is passed.
if(dataSnapshot.exists()){
lo = dataSnapshot.getValue(double.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
/////////////////////////////////////////////////////
updateLatestDistance(checkId, la, lo);
///showUpdateDidalog(device.getDeviceId(), device.getNickname());
}
//Create a new adapter
DeviceListAdapter a = new DeviceListAdapter(Activity_Main.this, devices);
listViewDevices.setAdapter(a);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是根据要求从 Firebase 控制台截取的屏幕截图。
这里是要求的 updateLatestDistance 方法。
//Function - Update the distance
private boolean updateLatestDistance(String deviceId, double safety_la, double safety_lo){
//Compute the latest distance between device's current location & selected safetyzone location.
//To provide real-time distance changing
//Firebase - Push latest distance.
DatabaseReference dbLatest_distance = FirebaseDatabase.getInstance().getReference("Device/" +deviceId+ "/distance");
DatabaseReference dbLatest_extra = FirebaseDatabase.getInstance().getReference("Device/" +deviceId+ "/extra");
//Check if safetyZone defined
if(safety_la!=0.0){
float[] new_d = new float[3];
//Compute the distance AGAIN.*
Location.distanceBetween( Double.parseDouble(latitude), Double.parseDouble(longtitude), safety_la, safety_lo, new_d);
//Push the distance data to the Firebase AGAIN.*
dbLatest_distance.setValue(new_d[0]+"m");
//If distance < 50m, trigger the alarm on the device (Raspberrypi)
//By Sending 'on' or 'off' to Firebase.
if(new_d[0] < 50){
dbLatest_extra.setValue("on");
}else{
dbLatest_extra.setValue("off");
}
}
return true;
}
【问题讨论】:
-
如果可能的话,发布一些你的 firebase 控制台的屏幕截图
-
@RahulChandrabhan Firebase 已更新。
-
添加更新最新距离方法的代码
-
@UmarHussain 嗨,我已经更新了方法的代码。
标签: java android firebase firebase-realtime-database