如果Firebase数据库没有子值,则开始下一个活动
我用于在活动A中填充Recycleview Adapter表单Firebase数据库.我正在寻找解决方案,以在其父级为空或子级为空时返回Mainactivity.
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.
这是我的代码
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();
}
}
我最初已经声明
int child_count=0;
但是,当它没有任何子值时,.still progressDialog会无限加载,除非按下任何键.
But also when it has no any child value..still progressDialog keeps loading infinitely unless any key is pressed.
感谢您的帮助.
onChildAdded()
仅在添加了新的子项或已有子项时被调用.
onChildAdded()
will only be called when a new child has been added or a child already exist.
在您的情况下,您没有添加任何子代,因此永远不会调用onChildAdded()
.这就是 progressDialog不断无限加载
In your case, you don't have any child added, So onChildAdded()
is never been called. that's why progressDialog keeps loading infinitely
要检测是否存在子代,请使用ValueEventListener
而不是ChildEventListener
.这是链接如何使用ValueEventListener
To detect if a child exists, use ValueEventListener
instead of ChildEventListener
. Here is link how to use ValueEventListener