Java 如何使用dataSnapshot.hasChildren()检查recyclerview是否为空
我正在使用firebasedatabase将我的数据显示到recyclerview中。当我的recyclerview为空并且我正在使用Java 如何使用dataSnapshot.hasChildren()检查recyclerview是否为空,java,android,firebase,Java,Android,Firebase,我正在使用firebasedatabase将我的数据显示到recyclerview中。当我的recyclerview为空并且我正在使用dataSnapshot.hasChildren()检查数据库hasChildren与否时,我想显示简单的toast。这是代码 private void Geofireinit() { started = true; databaseReference=FirebaseDatabase.getInstance().getReference().
dataSnapshot.hasChildren()
检查数据库hasChildren与否时,我想显示简单的toast。这是代码
private void Geofireinit() {
started = true;
databaseReference=FirebaseDatabase.getInstance().getReference().child(("tracker"));
GeoFire fire=new GeoFire(databaseReference);
GeoQuery geoQuery = fire.queryAtLocation(new GeoLocation(20.887715, 77.757623), 50);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.e("id", key);
getkeys(key);
}
@Override
public void onKeyExited(String key) {
Log.d("TAG", "onKeyExited: ");
for (int i=0;i<arrayList.size();i++)
{
Log.i("tte",String.valueOf(arrayList.get(i).getText()));
if (key.equals(arrayList.get(i).getText())) {
arrayList.remove(i);
adapter.notifyItemRemoved(i);
}
if (arrayList.size()==0)
{
Log.i("exited",String.valueOf(arrayList.size()));
}
}
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
ArrayList<Mylist> arrayList=new ArrayList<>();
private void getkeys(String key)
{
myref=FirebaseDatabase.getInstance().getReference().child("buses").child(key);
myref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
Mylist mylist=new Mylist();
mylist.setText(dataSnapshot.child("text").getValue().toString());
arrayList.add(mylist);
adapter=new Adapter(MainActivity.this,arrayList);
recyclerView.setAdapter(adapter);
Log.i("chaadad", String.valueOf(dataSnapshot.hasChild("buses")));
}
if (dataSnapshot.hasChildren())
{
// do something
}else
Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void Geofireinit(){
开始=真;
databaseReference=FirebaseDatabase.getInstance().getReference().child((“跟踪器”);
GeoFire=新的GeoFire(数据库参考);
GeoQuery GeoQuery=fire.queryLocation(新的地理位置(20.887715,77.757623),50);
geoQuery.addGeoQueryEventListener(新的GeoQueryEventListener(){
@凌驾
public void onKeyEntered(字符串键、地理位置){
Log.e(“id”,键);
获取密钥(密钥);
}
@凌驾
public void onKeyExited(字符串键){
Log.d(“TAG”,“onKeyExited:”);
对于(int i=0;i您可以使用
更新:
检查空列表的.exists()的else条件
if (dataSnapshot.exists())
{
Mylist mylist=new Mylist();
mylist.setText(dataSnapshot.child("text").getValue().toString());
arrayList.add(mylist);
adapter=new Adapter(MainActivity.this,arrayList);
recyclerView.setAdapter(adapter);
Log.i("chaadad", String.valueOf(dataSnapshot.hasChild("buses")));
} else
Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();
这段代码也给出了相同的输出,我还使用了if(arraylist.isEmpty)
只有当数据可用时,此功能才起作用,但当数据不可用时,否则两者都不可用work@prototype86请检查回答中的更新部分是,当数据可用时,如果条件被触发,但当数据不可用时,其他不执行。我不知道发生了什么事。我还检查了此not working else语句不工作if(dataSnapshot.exists())
在数据可用时工作正常,但当数据为空时,else条件不执行sir我不想使用addValueEventListener
因为它给我重复的值hh抱歉,如果我找不到您。不,先生,没问题
if (dataSnapshot.exists())
{
Mylist mylist=new Mylist();
mylist.setText(dataSnapshot.child("text").getValue().toString());
arrayList.add(mylist);
adapter=new Adapter(MainActivity.this,arrayList);
recyclerView.setAdapter(adapter);
Log.i("chaadad", String.valueOf(dataSnapshot.hasChild("buses")));
} else
Toast.makeText(MainActivity.this,"empty",Toast.LENGTH_SHORT).show();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if(!snapshot.hasChildren()){
// db has no children
}
// OR this way
if(snapshot.getChildrenCount() == 0){
// db has no children
}
// OR this way
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
// db has no children
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});