无法检索Firebase数据

时间:2018-05-11 19:02:24

标签: java android firebase firebase-realtime-database

好的,所以我正在尝试创建关注者/关注功能(不确定最佳方法),并且在从Firebase数据库检索数据时遇到问题。我正在使用recyclelerView显示所有用户,每个用户都有一个跟随/关注按钮。我遇到的问题是检查用户是否已经关注。我将详细说明下面的代码

ViewHolder for RecyclerView

@Override
        public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext())
                    .inflate(R.layout.artist_list_single, parent, false);
            TextView username = (TextView) view.findViewById(R.id.all_users_name);
            username.setTypeface(notes_font);

            return new UserViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull Users model) {

            final String current_uid = currentUser.getUid();
            final String user_uid = getRef(position).getKey();
            final String test = mUserDatabase.child(current_uid).child("following").child(user_uid).getKey();

            final Button follow_following = (Button) holder.mView.findViewById(R.id.follow_button);
            follow_following.setTypeface(notes_font);

            follow_following.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!test.equals(user_uid)) {
                        Toast.makeText(getActivity(), test, Toast.LENGTH_LONG).show();
                        mUserDatabase.child(current_uid).child("following").child(user_uid).setValue("true").addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    mUserDatabase.child(user_uid).child("follower").child(current_uid).setValue("true");
                                    follow_following.setBackgroundResource(R.drawable.login_button);
                                    follow_following.setTextColor(getResources().getColor(R.color.white));
                                    follow_following.setText("Following");
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    } else if (test.equals(user_uid)) {
                        Toast.makeText(getActivity(), test, Toast.LENGTH_LONG).show();
                        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity())
                                .setTitle("Unfollow?")
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mUserDatabase.child(current_uid).child("following").child(user_uid).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {
                                                    follow_following.setBackgroundResource(R.drawable.round_outline);
                                                    follow_following.setTextColor(getResources().getColor(R.color.app_theme));
                                                    follow_following.setText("Follow");
                                                } else {
                                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                                }
                                            }
                                        });
                                    }
                                })
                                .setNegativeButton("No", null)
                                .setCancelable(false);
                        dialog.show();
                    }
                }
            });

                holder.setName(model.getName());
                holder.setThumbImage(model.getThumbImage());
                holder.setStatus(model.getStatus());


            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent profileIntent = new Intent(getActivity(), All_Profiles.class);
                    profileIntent.putExtra("user_uid", user_uid);
                    startActivity(profileIntent);
                }
            });
        }

    };

因此,在检索测试字符串时,无论User_uid是否在数据库中,它都会检测到test.equals(user_uid)为true ..不确定我在哪里出错了。 接下来我尝试使用ValueEventListening来检索onDataChange()的数据 像这样

@Override
        protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull Users model) {

            final String current_uid = currentUser.getUid();
            final String user_uid = getRef(position).getKey();
            final String test = mUserDatabase.child(current_uid).child("following").child(user_uid).getKey();

            mUserDatabase.child(current_uid).child("following").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    testing = dataSnapshot.child(user_uid).getKey();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            final Button follow_following = (Button) holder.mView.findViewById(R.id.follow_button);
            follow_following.setTypeface(notes_font);

            follow_following.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mUserDatabase.child(current_uid).child("following").addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            testing = dataSnapshot.child(user_uid).getKey();
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                    if (!testing.equals(user_uid)) {
                        Toast.makeText(getActivity(), testing, Toast.LENGTH_LONG).show();
                        mUserDatabase.child(current_uid).child("following").child(user_uid).setValue("true").addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    mUserDatabase.child(user_uid).child("follower").child(current_uid).setValue("true");
                                    follow_following.setBackgroundResource(R.drawable.login_button);
                                    follow_following.setTextColor(getResources().getColor(R.color.white));
                                    follow_following.setText("Following");
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    } else if (testing.equals(user_uid)) {
                        Toast.makeText(getActivity(), testing, Toast.LENGTH_LONG).show();
                        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity())
                                .setTitle("Unfollow?")
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mUserDatabase.child(current_uid).child("following").child(user_uid).setValue("false").addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {
                                                    follow_following.setBackgroundResource(R.drawable.round_outline);
                                                    follow_following.setTextColor(getResources().getColor(R.color.app_theme));
                                                    follow_following.setText("Follow");
                                                } else {
                                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                                }
                                            }
                                        });
                                    }
                                })
                                .setNegativeButton("No", null)
                                .setCancelable(false);
                        dialog.show();
                    }
                }
            });

                holder.setName(model.getName());
                holder.setThumbImage(model.getThumbImage());
                holder.setStatus(model.getStatus());


            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent profileIntent = new Intent(getActivity(), All_Profiles.class);
                    profileIntent.putExtra("user_uid", user_uid);
                    startActivity(profileIntent);
                }
            });
        }

注意:我调用了两次,因为在onClick之前只调用一次因为某种原因只检索了第一个用户的密钥。并且在每次onClick崩溃期间只调用一次。称它为我两次工作除了每次按下按钮它不更新钥匙直到我点击它两次..总而言之我真的很茫然。请原谅我的无知,因为我还在学习,我知道我的代码不完美(甚至关闭大声笑)所以请记住这一点。另外,我一直在阅读关于Firebase和Android的官方文档,关于我现在要做的一周以上的事情,如果我不这样做,我就不会来这里。感谢所有人提供的任何帮助:)

这是我的数据库的图片 Firebase Database

0 个答案:

没有答案