我的 MainActivity.java 有问题,当我将 FirebaseRecycler 适配器放入 OnStart()r 中时,OnStart() 方法不会调用 CheckUserExistence()

问题描述 投票:0回答:1

我的 MainActivity.java 代码中有一个问题,我在其中显示所有用户帖子,问题出在我的 onStart() 方法中,当我将 FirebaseRecyclerAdapter 放入 onStart() 方法中时,onStart() 不会调用 CheckUserExistence()方法位于 OnStart() 方法中,该方法将用户引导至 SetUpActivity.java,一旦我删除 FirebaseRecyclerAdapter,它就可以正常工作,意味着它调用 CheckUserExistence() 方法并完美地转到 SetupActivity,出了什么问题,我不能弄清楚,请帮忙-

这是 MainActivity.java 中我遇到问题的特定代码部分 -



    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser == null) {
            SendUserToLoginActivity();
        } else {
            CheckUserExistence();
        }

        Query SortPostsInDescendingOrder = PostsRef.orderByChild("counter");

        FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPostsInDescendingOrder, Posts.class).build();

        FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
                (options) {
            @Override
            protected void onBindViewHolder(@NonNull PostsViewHolder postsViewHolder, int i, @NonNull Posts posts) {

             final String PostKey = getRef(i).getKey();

                postsViewHolder.username.setText(posts.getFullname());
                postsViewHolder.PostTime.setText(posts.getTime());
                postsViewHolder.PostDate.setText(posts.getDate());
                postsViewHolder.PostDescription.setText(posts.getDescription());
                Picasso.get().load(posts.getProfileimage()).into(postsViewHolder.image);
                Picasso.get().load(posts.getPostimage()).into(postsViewHolder.Postimage);

                postsViewHolder.setLikeButtonStatus(PostKey);

                postsViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent clickPostIntent = new Intent(MainActivity.this, ClickPostActivity.class);
                        clickPostIntent.putExtra("PostKey", PostKey);
                        startActivity(clickPostIntent);



                    }
                });



                postsViewHolder.LikePostButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LikeChecker = true;

                        LikesRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {

                              if(LikeChecker.equals(true))
                              {
                                  if(snapshot.child(PostKey).hasChild(currentUserID))
                                  {
                                      LikesRef.child(PostKey).child(currentUserID).removeValue();
                                      LikeChecker = false;
                                  }
                                  else
                                  {
                                      LikesRef.child(PostKey).child(currentUserID).setValue(true);
                                      LikeChecker = false;
                                  }

                              }

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                    }
                });


            }

            @NonNull
            @Override
            public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_posts_layout, parent, false);
                PostsViewHolder viewHolder = new PostsViewHolder(view);
                return viewHolder;
            }
        };






      postList.setAdapter(firebaseRecyclerAdapter);
      firebaseRecyclerAdapter.startListening();

      updateUserStatus("online");

    }








    private void CheckUserExistence() {

            final String current_user_id = mAuth.getCurrentUser().getUid();


            UsersRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    if (!snapshot.hasChild(current_user_id)) {
                        SendUserToSetupActivity();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }



    private void SendUserToSetupActivity() {
        Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
        setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(setupIntent);
        finish();

    }

  













我从 onStart() 方法中删除了 FirebaseRecyclerAdapter,它工作正常,这意味着它引导用户访问 SetupActivity.java,按照预期代码,我还尝试扩展 MainActivity。 java 并做了另一个活动只是为了显示帖子,这样 onStart 就不会冲突,但这是完全错误的,因为我的应用程序甚至没有启动。

当我从 onStart 方法中删除 FirebaseRecyclerAdapter 时,onStart() 方法工作正常,这意味着它会将用户引导至 SetupActivity.java 作为预期代码 -

 protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if (currentUser == null) {
            SendUserToLoginActivity();
        } else {
            CheckUserExistence();
        }
    }



    private void CheckUserExistence() {
        final String current_user_id = mAuth.getCurrentUser().getUid();

        UsersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (!dataSnapshot.hasChild(current_user_id)) {
                    SendUserToSetupActivity();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


但是当我在 onStart() 方法中添加 FirebaseRecyclerAdapter 时,它无法按预期工作意味着它没有进入 SetupActivity.java -

 protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser == null) {
            SendUserToLoginActivity();
        } else {
            CheckUserExistence();
        }

        Query SortPostsInDescendingOrder = PostsRef.orderByChild("counter");

        FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPostsInDescendingOrder, Posts.class).build();

        FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
                (options) {
            @Override
            protected void onBindViewHolder(@NonNull PostsViewHolder postsViewHolder, int i, @NonNull Posts posts) {

             final String PostKey = getRef(i).getKey();

                postsViewHolder.username.setText(posts.getFullname());
                postsViewHolder.PostTime.setText(posts.getTime());
                postsViewHolder.PostDate.setText(posts.getDate());
                postsViewHolder.PostDescription.setText(posts.getDescription());
                Picasso.get().load(posts.getProfileimage()).into(postsViewHolder.image);
                Picasso.get().load(posts.getPostimage()).into(postsViewHolder.Postimage);

                postsViewHolder.setLikeButtonStatus(PostKey);

                postsViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent clickPostIntent = new Intent(MainActivity.this, ClickPostActivity.class);
                        clickPostIntent.putExtra("PostKey", PostKey);
                        startActivity(clickPostIntent);



                    }
                });



                postsViewHolder.LikePostButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LikeChecker = true;

                        LikesRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {

                              if(LikeChecker.equals(true))
                              {
                                  if(snapshot.child(PostKey).hasChild(currentUserID))
                                  {
                                      LikesRef.child(PostKey).child(currentUserID).removeValue();
                                      LikeChecker = false;
                                  }
                                  else
                                  {
                                      LikesRef.child(PostKey).child(currentUserID).setValue(true);
                                      LikeChecker = false;
                                  }

                              }

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                    }
                });


            }

            @NonNull
            @Override
            public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_posts_layout, parent, false);
                PostsViewHolder viewHolder = new PostsViewHolder(view);
                return viewHolder;
            }
        };






      postList.setAdapter(firebaseRecyclerAdapter);
      firebaseRecyclerAdapter.startListening();

      updateUserStatus("online");

    }


private void CheckUserExistence() {

            final String current_user_id = mAuth.getCurrentUser().getUid();


            UsersRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    if (!snapshot.hasChild(current_user_id)) {
                        SendUserToSetupActivity();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }

















请帮忙。请详细告诉我我的代码哪里出了问题,并进行适当的代码改进和详细实现。

java android firebase-realtime-database android-recyclerview firebase-authentication
1个回答
0
投票

我已经解决了这个错误,我一直在寻找将用户从Mainactivity发送到SetupActivity的方法,但后来我决定可以将用户直接从LoginActivity发送到SetupActivity,不需要使用可以使用常识的复杂实践。

© www.soinside.com 2019 - 2024. All rights reserved.