我用于在活动A中填充Recycleview Adapter表单firebase数据库。我正在寻找解决方案,以在其父级为空或子级为空时返回Mainactivity。
这是我的代码
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会无限加载,除非按下任何键。
感谢您的帮助。
答案 0 :(得分:1)
onChildAdded()
仅在添加了新的孩子或孩子已经存在时被调用。
在您的情况下,您没有添加任何子级,因此永远不会调用onChildAdded()
。这就是为什么 progressDialog保持无限加载
要检测是否存在孩子,请使用ValueEventListener
而不是ChildEventListener
。这是链接how to use ValueEventListener
答案 1 :(得分:1)
您可以使用exist检查引用数据库引用的路径是否存在
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()返回布尔值
如果此DataSnapshot包含任何数据,则返回true。稍微 比使用snapshot.val()!== null更有效。
这将检查dbReference
所指出的路径内是否有数据,但不会检查其中的子级是否大于0。
为此,您可以在else语句之后检查该节点内是否有多个子节点
...
}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();
}
}