我如何从Firebase获取多个密钥

时间:2019-02-22 13:24:12

标签: android firebase firebase-realtime-database

![在此处输入图片描述] [1]

 mAdapter = new FirebaseRecyclerAdapter<Adapter_Hotels, Adapter_HotelsHolder>(mOptions) {
            @Override
            protected void onBindViewHolder(@NonNull final Adapter_HotelsHolder holder, int position, @NonNull Adapter_Hotels model) {

            String key = getRef(position).getKey();
            mRef1 = mRef.child(key).child("rooms");
            mRef1.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        roomkey = child.getKey();
                    }
                    minmax.clear();
                    mRef2 = mRef1.child(roomkey).child("promos").child("regular").child(ym).child("price").child(guest);
                    mRef2.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                            }

                        }

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

                        }
                    });
                }

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

                }
            });

        }

这是我所有的查询。这是明智的吗?我删除了所有检索代码,这完全正常。我的问题是还有其他更好或更清洁的方法吗?

2 个答案:

答案 0 :(得分:1)

尝试一下:

awk -F';' '                           ##Starting awk program here and setting up field separator as semi-colon here.
{
  val=""                              ##Nullifying value of variable val here.
  for(i=1;i<=NF;i++){                 ##using a for loop which starts from i=1 to i=NF value. Where NF is number of fields value in current line.
     if($i ~ /^AF_female=[0-9]+/){    ##Checking condition if a field starts from AF_female and then digits then do following.
         val=(val?val OFS $i:$i)      ##Creating variable val whose value is current field value and concatenating its own value.
     }
  }
  if(val!=""){                        ##After coming out of for loop checking if variable val value is NOT NULL then do following.
     print val                        ##Printing value of variable val here.
  }
  else{                               ##Mentioning else of above if condition here.
     print "NA"                       ##Printing NA here.
  }
}' Input_file                         ##Mentioning Input_file name here.

答案 1 :(得分:1)

您的代码看起来比所需的复杂。据我所知,您可以使用以下方法完成完全相同的操作:

mRef1 = mRef.child(key).child("rooms");
mRef1.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot roomSnapshot: dataSnapshot.getChildren()) {
            roomkey = child.getKey();
            DataSnapshot priceSnapshot = roomSnapshot.child(roomkey).child("promos").child("regular").child(ym).child("price").child(guest);
            System.out.println(priceSnapshot.getValue());
        });
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

更改:

  • 请不要将onCancelled留空,因为未发现的错误确实很难排除。
  • 将查询价格放入循环中,以便查询所有房间的价格。如果您只想查询一间房的价格,则应使用查询仅读取该单间的价格。
  • 价格本身不需要使用单独的侦听器,因为(据我所知)价格已经在dataSnapshot的{​​{1}}中。