Start Next Activity If Firebase Database Has No Child Value
Solution 1:
onChildAdded()
will only be called when a new child has been added or a child already exist.
In your case, you don't have any child added, So onChildAdded()
is never been called. that's why progressDialog keeps loading infinitely
To detect if a child exists, use ValueEventListener
instead of ChildEventListener
. Here is link how to use ValueEventListener
Solution 2:
You can use exists to check if the path that refers your database reference exists
dbreference.addChildEventListener(new ChildEventListener() {
@Override
publicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
if(!dataSnapshot.exists()){
final Books b1 = dataSnapshot.getValue(Books.class);
// Log.e("Value is ",dataSnapshot.getKey()+" "+b1.getBauthor());//Log.e("Book"," received");if(!user.equals(b1.getSelleremail()) || (user.equals(b1.getSelleremail())) ) {
child_count++;
list.add(b1);
staggeredBooksAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}else{
progressDialog.dismiss();
Toast.makeText(SubjectBooks.this, "No books found!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(SubjectBooks.this,MainActivity.class);
startActivity(in);
finish();
}
}
exists
exists() returns boolean
Returns true if this DataSnapshot contains any data. It is slightly more efficient than using snapshot.val() !== null.
That will check if there is data inside the path your dbReference
points out, but it will not check if the childs inside are > 0.
To do so you can check after the else statment if inside that node you have more than 1 child node
...
}else{
long childrenQty = dataSnapshot.getChildrenCount();;
if(childrenQty > 0 ) {
//We have >= than 1 children to show in our recyclerView :-)
}else{
//Theres nothing inside to show in our recyclerView :-(
progressDialog.dismiss();
Toast.makeText(SubjectBooks.this, "No books found!", Toast.LENGTH_SHORT).show();
Intent in = newIntent(SubjectBooks.this,MainActivity.class);
startActivity(in);
finish();
}
}
Post a Comment for "Start Next Activity If Firebase Database Has No Child Value"