检查字段数据是否更改,而不是子数据中的任何字段 [英] Checking if field data changed rather than any field in child data
问题描述
我可以检查是否有任何数据是我的引用的孩子改变了没有问题,但我无法找到一种方法来检查某个字段是否已经改变了该孩子。
下面是一些代码.child(job)不工作,因为工作是这个领域。这只是我尝试的事情。
Firebase ref = new Firebase(Config.FIREBASE_URL);
//添加一个值事件监听器,所以如果数据库中的数据改变它在textview中也不需要在分钟
ref.child(Driver)。child(Driver2) (DataSnapshot快照){
for(DataSnapshot postSnapshot:snapshot.getChildren(job))addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange )){
Toast.makeText(ProfileActivity.this,YEAAA,Toast.LENGTH_SHORT).show();
}
}
/ ************必须执行这个方法**************** /
@Override
public void onCancelled(FirebaseError firebaseError){
System.out.println(The read failed:+ firebaseError.getMessage());
}
});
这里是数据库中的json
Driver:{
Driver1:{
job:none,
lat:53.4013419,
lon:-111.803055,
name:JOHNNY
},
Driver2:{
job:job,
lat:53.4012956,
lon:-6.4091191,
name:MICK
},
Driver3:{
job: cccc,
lat:45.815399,
lon:15.966568,
name:Boris
}
}
我想检查工作字段是否改变,因为坐标字段会不断变化。 $ b
引用中正确使用 addValueEventListener()
code>但是你不需要 for循环
。您的 DataSnapshot
已经在作业子
中,其中的键是作业
,值为: none
。所以你需要删除迭代。希望它有帮助。
I can check if any data that is a child of my reference has changed no problem but I cant find a way to check if a certain field has changed within that child.
Here is some code the .child("job") does not work as job is the field. It was just somethin I was trying.
Firebase ref = new Firebase(Config.FIREBASE_URL);
//adding a value event listener so if data in database changes it does in textview also not needed at the minute
ref.child("Driver").child("Driver2").child("job").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Toast.makeText(ProfileActivity.this, "YEAAA ", Toast.LENGTH_SHORT).show();
}
}
/************had to implement this method****************/
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
here is the json from the database
"Driver" : {
"Driver1" : {
"job" : "none",
"lat" : 53.4013419,
"lon" : -111.803055,
"name" : "JOHNNY"
},
"Driver2" : {
"job" : "job",
"lat" : 53.4012956,
"lon" : -6.4091191,
"name" : "MICK"
},
"Driver3" : {
"job" : "cccc",
"lat" : 45.815399,
"lon" : 15.966568,
"name" : "Boris"
}
}
I want to check if the job field changes, as the coordinates fields will be constantly changing anyway.
解决方案 You are using correctly addValueEventListener()
on the reference
but you don't need that for loop
. Your DataSnapshot
is already on the job child
, in which the key is job
and the value is: none
. So you need to remove the iteration. Hope it helps.
这篇关于检查字段数据是否更改,而不是子数据中的任何字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!