我陷入一个问题。我的firebase结构在图像中给出,其中subject是我从按钮上单击时使用意图额外得到的子值
考虑我有两个按钮Button A和Button B
当我按下按钮A时,它将主题发送为“ Bhaktapur”的字符串值,现在我在Recycleview中有Bhaktapur的详细信息,但是当我扣住按钮B时,如果它传递了字符串值“ xyz”,则我没有任何值带有数据“ xyz”,则应返回Toast消息,并返回“找不到”
但是第一个可以正常工作,并且用信息更新了recycleview,但是在第二种情况下,我有进度对话框一直加载,直到我取消它为止。
这是我的代码
dbreference =
FirebaseDatabase.getInstance().getReference("books").child(subject);
dbreference.addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
if (!data.exists()) {
progressDialog.dismiss();
Toast.makeText(SubjectBooks.this, "No books
found!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(SubjectBooks.this,
MainActivity.class);
startActivity(in);
finish();
} else {
final Books b1 = data.getValue(Books.class);
// Log.e("Value is ",dataSnapshot.getKey()+"
"+b1.getBauthor());
//Log.e("Book"," received");
child_count++;
list.add(b1);
staggeredBooksAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
}
答案 0 :(得分:1)
如果按钮B传递了字符串值“ xyz”,我将单击按钮B,那么我没有任何按钮 带有数据“ xyz”的值,则应返回Toast with Toast 消息“未找到”
由于找不到针对xyz
的数据,for loop
也不会执行if-else
。由于您关闭if-else
内部的进度条,因此它将无限期显示。因此,您需要按照以下步骤检查DataSnapshot
之前是否存在for loop
dbreference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
// TODO: handle the case where the data exists
for (DataSnapshot data : snapshot.getChildren()) {
final Books b1 = data.getValue(Books.class);
// Log.e("Value is ",dataSnapshot.getKey()+"
"+b1.getBauthor());
//Log.e("Book"," received");
child_count++;
list.add(b1);
staggeredBooksAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
else {
// TODO: handle the case where the data does not yet exist
progressDialog.dismiss();
Toast.makeText(SubjectBooks.this, "No books
found!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(SubjectBooks.this,
MainActivity.class);
startActivity(in);
finish();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) { }
});