问题
I am using to populate the Recycleview Adapter form firebase database in Activity A. I am looking for solution to return to Mainactivity when it has empty parent or no child.
Here is my code
public class SubjectBooks extends AppCompatActivity {
FirebaseAuth fauth;
String user;
DatabaseReference dbreference,dbref;
RecyclerView rv;
String subject;
int child_count=0;
ArrayList<Books> list;
SubjectBooksAdapter staggeredBooksAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_books);
fauth = FirebaseAuth.getInstance();
subject = getIntent().getStringExtra("subject").trim();
dbreference = FirebaseDatabase.getInstance().getReference("books").child(subject);
dbreference.keepSynced(true);
user = fauth.getCurrentUser().getEmail();
final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading...", "Please wait...", true);
progressDialog.setMessage("Please wait ...");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.show();
dbreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
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();
}
if(child_count==0){
progressDialog.dismiss();
Toast.makeText(SubjectBooks.this, "No books found!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(SubjectBooks.this,MainActivity.class);
startActivity(in);
finish();
}
}
I have initially declared
int child_count=0;
But also when it has no any child value..still progressDialog keeps loading infinitely unless any key is pressed.
Any help is appreciated.
回答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
回答2:
You can use exists to check if the path that refers your database reference exists
dbreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(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 = new Intent(SubjectBooks.this,MainActivity.class);
startActivity(in);
finish();
}
}
来源:https://stackoverflow.com/questions/54165807/start-next-activity-if-firebase-database-has-no-child-value