按签名的用户显示子级

时间:2019-05-31 12:45:38

标签: android firebase firebase-realtime-database

你好,我是firebase的新手,我需要firebase的显示子子级的帮助,该子级包含与已签名用户相同的userID

here is my database  here is users and edu1 full show of user 我只需要显示已签名用户的子孩子

这是我的代码  私有FirebaseAuth fAuth;

private DatabaseReference fNotesDatabase;
ArrayAdapter adapter;
ListView listView;
ArrayList<String> arraylist = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_education);
    fAuth = FirebaseAuth.getInstance();
    listView = findViewById(R.id.listview);
    fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");


    fNotesDatabase.addListenerForSingleValueEvent(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot datas: dataSnapshot.getChildren()){
                String values = datas.getKey();
                arraylist.add(values);
                adapter = new ArrayAdapter(Education.this, android.R.layout.simple_list_item_single_choice, arraylist);
                listView.setAdapter(adapter);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

3 个答案:

答案 0 :(得分:0)

尝试以下操作:

    FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    String userId = currentFirebaseUser.getUid();
    fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");


    fNotesDatabase.addListenerForSingleValueEvent(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot datas: dataSnapshot.getChildren()){
                String values = datas.getKey();

               db = FirebaseDatabase.getInstance().getReference().child(values).orderByKey().equalTo(userId);
               db.addListenerForSingleValueEvent(new ValueEventListener(){
                   @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
                   //get data
             }

                    @Override
                public void onCancelled(DatabaseError databaseError) {

                }
             });

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

在这里您添加对education的引用,然后在education下检索直接子级,然后使用orderByKey()来检查是否存在与当前登录用户相同的密钥并检索数据

答案 1 :(得分:0)

您可以尝试使用此代码,看看它是否与您一起运行:

FirebaseAuth mAuth = FirebaseAuth.getInstance();
 // get the current user-id to read data easly
String  currentUserId = mAuth.getCurrentUser().getUid();
DatabaseReference userRef = FirebaseDatabase.getInstance()             
   .getReference().child("notes").child("education");
 // get data to them
userRef.child(currentUserId).addValueEventListener(
            new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
      if(dataSnapshot.exists()){
         for(DataSnapshot datas: dataSnapshot.getChildren()){
            String values = datas.getKey();
            arraylist.add(values);
            adapter = new ArrayAdapter(Education.this,
             android.R.layout.simple_list_item_single_choice, 
             arraylist);
            listView.setAdapter(adapter);

         }
      }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

        }
});

答案 2 :(得分:0)

您似乎具有类似/Education/Notes/$uid的数据结构。因此,只需阅读当前登录用户的注释,即可:

fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child("education");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

fNotesDatabase.child(uid).addListenerForSingleValueEvent(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ...