用于RecyclerView的Firebase查询

时间:2018-09-29 11:58:02

标签: android firebase firebase-realtime-database

我想得到第一个孩子的朋友日期。为此,我将查询写为enter image description here

Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Friends").child("SQyOq80egYehjqx4sgiyeNcW8P02");

在上述查询中,我要具体获取“ SQyOq80egYehjqx4sgiyeNcW8P02”的日期。我怎样才能做到这一点?上面的查询无法正常工作。

我以前是通过以下代码编写这些值的:

 final String currentDate = DateFormat.getDateInstance().format(new Date());
                    mFriendDatabase.child(mCurrent_user.getUid()).child(user_id).setValue(currentDate)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                    mFriendDatabase.child(user_id).child(mCurrent_user.getUid()).setValue(currentDate)
                                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    mFriendReqDatabase.child(mCurrent_user.getUid()).child(user_id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                                                        @Override
                                                        public void onSuccess(Void aVoid) {

                                                            mFriendReqDatabase.child(user_id).child(mCurrent_user.getUid()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
                                                                @Override
                                                                public void onSuccess(Void aVoid) {


                                                                    mProfileSendReqBtn.setEnabled(true);
                                                                    mCurrent_state = "friends";
                                                                    mProfileSendReqBtn.setText("UnFriend");

                                                                    mDeclineBtn.setVisibility(View.INVISIBLE);
                                                                    mDeclineBtn.setEnabled(false);


                                                                }
                                                            });
                                                        }
                                                    });
                                                }
                                            });
                                    }
                                });

                    }

回收站代码:

FirebaseRecyclerOptions<Friends> options =
                new FirebaseRecyclerOptions.Builder<Friends>()
                        .setQuery(query, Friends.class)
                        .build();

        Log.d("sdfsdfsdf", options.getSnapshots().toString());

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(options) {
            @Override
            public FriendsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_users_activity_layout, parent, false);

                return new FriendsViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(final FriendsViewHolder friendsViewHolder, int position, Friends friends) {
//                Log.d("sdfsdfdf", friends.getDate().toString());
                friendsViewHolder.setDate(friends.getDate());

2 个答案:

答案 0 :(得分:1)

如果您想使用Query

寻找一些价值
Query applyQuery = ref.child("Friends").orderByChild("Wed5qPTC.....").equalTo("Value for Check");
applyQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snap: dataSnapshot.getChildren()) {
            //You will get your value in snap
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e("Realtime", "onCancelled", databaseError.toException());
    }
});

答案 1 :(得分:1)

假设SQyOq80egYehjqx4sgiyeNcW8P02是已登录用户的ID,要获取wed5 ... 43v1的值,请使用以下代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Friends").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String date = ds.getValue(String.class);
            Log.d(TAG, date);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

您的logcat中的输出将是:

Sept 29, 2018

如果SQyOq80egYehjqx4sgiyeNcW8P02是另一个随机生成的ID,在这种情况下,请使用以下参考:

DatabaseReference uidRef = rootRef.child("Friends").child("SQyOq80egYehjqx4sgiyeNcW8P02");