如何从火力基地中检索数据并分配给字符串

时间:2018-10-22 06:33:51

标签: android firebase firebase-realtime-database

我有一个基于数据库的数据库。因为我有一个名为“ item1”的子节点,在item1下,我有两个值

  1. 名称
  2. 奖章

我想检索item1的名称,并将其放在名为“ foodname”的字符串中。我该怎么办?

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");

我在这里尝试过但没有找到解决方法

String foodname; //this string should get the value as "diary milk"
mDatabase.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
     }

     @Override
     public void onCancelled(DatabaseError databaseError) {
     }
 });

image of database

3 个答案:

答案 0 :(得分:3)

    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

    mDatabase.child("menu").addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {

         //This will loop through all items. Add variables to arrays or lists as required
         for (DatasnapShot snap : dataSnapshot.getChildren())
         {
             foodname = dataSnapshot.child("name").getValue().toString();
             String prize = dataSnapshot.child("prize").getValue().toString();
         }


     }

     @Override
     public void onCancelled(DatabaseError databaseError) {
     }
 });

您可以像这样分别获取每个孩子。或者,您可以使用模型或哈希图来获取所有数据,然后根据Key来获取所需的数据

答案 1 :(得分:1)

使用下面的代码,您可以获得food name

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("menu");

  mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                   String foodName = snapshot.child("name").getValue().toString();
                   String foodPrice = snapshot.child("prize").getValue().toString();        
               }
          }

     @Override
     public void onCancelled(DatabaseError databaseError) {

          }
 });

答案 2 :(得分:0)

从Firebase检索任何数据都需要正确的数据库引用和eventListeners才能满足您的需要。

进一步了解eventListeners visit this link.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("menu").child("item1");

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// this code will get only details from item1
                   foodname = dataSnapshot.child("name").getValue(String.class);
                   String price = dataSnapshot.child("prize").getValue(Integer.class);

//if you want details from multiple items, you have to loop over the child nodes like this

                   for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                       foodname = snapshot.child("name").getValue().toString();
                       String price = snapshot.child("prize").getValue().toString();        
                   }

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

              }
        });