如果Firebase数据库没有子值,则开始下一个活动 [英] Start next activity if firebase database has no child value

查看:47
本文介绍了如果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

这篇关于如果Firebase数据库没有子值,则开始下一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆