如何从Firebase中检索数据并显示用户名?

时间:2018-02-05 17:26:45

标签: java android firebase firebase-realtime-database

我尝试使用此代码检索并显示用户名,但每个节点都有唯一的ID。

Firebase MainFirebase;
private Button btnProfile;
private Button btnLibrary;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnProfile = (Button) findViewById(R.id.btnProfile);
    btnLibrary = (Button) findViewById(R.id.btnLibrary);
    final TextView FullName = (TextView) findViewById(R.id.TVFullName);

    Firebase.setAndroidContext(getApplicationContext());

    MainFirebase = new Firebase("https://avastus-mcphil.firebaseio.com/Name");

    MainFirebase.addValueEventListener(new com.firebase.client.ValueEventListener() {
        @Override
        public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {

            String myChildText = dataSnapshot.getValue(String.class);
            FullName.setText(myChildText);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

            FullName.setText("Error Found!");

        }
    });

检索推送数据的最佳方法是什么?

这就是我的数据库的样子:

  

-L4ampm6pwMeWA9X7A20

     

电子邮件:" mcphil@haha.com"

     

姓名:" Avastus"

1 个答案:

答案 0 :(得分:1)

试试这个:

   library(broom)
    d <- data.frame(group=rep(1:5, each = 20),
                    price=rnorm(100,1000,10),
                    stars=rnorm(100,3,1))
    fitted_models <- d %>% 
      group_by(group) %>% 
      do(model = lm(price ~ stars, data = .))
    fitted_models %>% tidy(model)
    fitted_models %>% glance(model)

假设推送ID在根节点下。

添加检索名称,假设数据库中的名称为 DatabaseReference ref=FirebaseDatabase.getInstance().getReference(); ref.addValueEventListener(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot datas: dataSnapshot.getChildren()){ String keys=datas.getKey(); String displayname=datas.child("Name").getValue().toString(); } } @Override public void onCancelled(FirebaseError firebaseError) { } });