如何从Firebase中的实时数据库检索数据?

时间:2019-04-24 19:48:37

标签: java android firebase firebase-realtime-database

我正在开发一个应用程序,用户在其中注册,输入一些信息,然后将其重定向到另一个页面,他们可以在其中看到输入的信息。信息使用Firebase保存在实时数据库中,我的问题是如何检索要显示的数据?我看过教程,但是我不明白如何在数组列表或类似的列表中检索这些数据。

这是我的数据库结构,其中id是用户的uuid: https://imgur.com/a/orNqyeq

这是我将数据保存到数据库的方式:

这是保存不同变量的类构造器

  public PersonalInfo(String Id, String Name, String Birthday, String weight, String height, String gender, String Protein, String carbs, String fat, String Weightloss)
    {
        this.id = Id;
        this.Name = Name;
        this.Birthday = Birthday;
        this.weight = weight;
        this.height = height;
        this.gender = gender;
        this.Protein = Protein;
        this.carbs = carbs;
        this.fat = fat;
        this.weightloss = Weightloss;
    }

,并在活动中获得不同文本字段的文本后 数据保存如下:

  PersonalInfo info = new PersonalInfo(Id, Name, Birthday, Weight, Height, Gender, Protein, Carbs, Fat, Weightloss);

        databaseinfo.child(Id).setValue(info);
        Toast.makeText(this, "Data added", Toast.LENGTH_SHORT).show();


如果任何人都可以帮助或给我提供解释我的问题的教程的链接,那将是非常有用的。

1 个答案:

答案 0 :(得分:1)

您好,只需将所有这些添加到方法中,然后调用它,并将'server/saving-data/fireblog/posts'替换为服务器节点链接,您将获得该节点的响应。

// Get a reference to our posts
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");

// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    Post post = dataSnapshot.getValue(PersonalInfo.class);
    System.out.println(post);
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});

存在几种类型的事件侦听器,您可以从here阅读有关数据检索的所有信息。

相关问题