如何计算父母子女中的孩子数

时间:2017-11-06 15:53:52

标签: java android firebase-realtime-database children

database rules

my database

我在firebase数据库中写入数据时遇到问题 它必须使用postId()方法为数据提供id 有时候它给了我一个正确的id,另一次错误的id或updata数据被创建了

对于所有用户帖子,id必须是这样的

[USER1]

postId1

postId2

postId3

postId4

postId5

postId100000

[USER2]

postId1

postId2

postId3

postId4

postId5

postId100000

............................................... .........................

        if (auth.getCurrentUser() != null) {


            mDatabase.child("AdUsersINFO").child(auth.getCurrentUser().getUid()).child(POSTID).setValue(adver,
                    new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                            if (databaseError == null) {


                                Toast.makeText(getContext(), "Data is saved successfully", Toast.LENGTH_LONG).show();

                            } else {

                                Toast.makeText(getContext(), "Data isn,t saved , please try again", Toast.LENGTH_LONG).show();
                            }

                        }
                    });

            mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO").child(auth.getCurrentUser().getUid()).child(POSTID);
            mAdUserDatabse.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    mAdUserDatabse.child("userid").setValue(auth.getCurrentUser().getUid());
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

private void postId(){

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");
    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.hasChild(auth.getCurrentUser().getUid())) {
                // run some code

                mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");
                mAdUserDatabse.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snap : dataSnapshot.getChildren()) {
                            Log.e(snap.getKey(), snap.getChildrenCount() + "");

                            if (snap.getChildrenCount() != 0) {

                                postId = "postId" + String.valueOf(snap.getChildrenCount() + 1);
                                SharedPreferencesUtils.setStringPreference(getActivity(), "postId", postId);

                            } else {
                                postId = "postId" + String.valueOf(1);
                                SharedPreferencesUtils.setStringPreference(getActivity(), "postId", postId);
                            }


                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            } else {
                postId = "postId" + String.valueOf(1);
                SharedPreferencesUtils.setStringPreference(getActivity(), "postId", postId);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

请拯救我的生命

1 个答案:

答案 0 :(得分:0)

您一次又一次地运行循环并覆盖postId的值,因此postId等于最后一个孩子的计数。因此,您始终获得最后一位用户的postId计数。

再看一下

部分
for (DataSnapshot snap : dataSnapshot.getChildren()) {
                        Log.e(snap.getKey(), snap.getChildrenCount() + "");

                        if (snap.getChildrenCount() != 0) {

                            postId = "postId" + String.valueOf(snap.getChildrenCount() + 1);
                            SharedPreferencesUtils.setStringPreference(getActivity(), "postId", postId);

                        } else {
                            postId = "postId" + String.valueOf(1);
                            SharedPreferencesUtils.setStringPreference(getActivity(), "postId", postId);
                        }


                    }

考虑为每个用户postId使用单独的密钥,或者为一个用户创建一个类似于此的密钥

public void getPostId(String userId) {
    final String postId = "post";
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO").child(userId);
    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            long c = snapshot.getChildrenCount();
            if(c==) postId = postId+"1";
            else postId = postId+ c+1;
        },

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
};