如何在Firebase中将数据检索到Android Studio

时间:2018-07-16 09:37:37

标签: java android firebase firebase-realtime-database

  1. 我需要从数据库Firebase检索数据。但是我有问题,因为当我尝试检索数据时,应用程序将停止。

  2. 这是restoredPoint.java的代码

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference("reloadPoint");
    
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ReloadPoint rp = dataSnapshot.getValue(ReloadPoint.class);
            System.out.println(rp);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });
    
    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ReloadPoint newPost = dataSnapshot.getValue(ReloadPoint.class);
            System.out.println("Email: " + newPost.getmEmail());
            System.out.println("Point: " + newPost.getmPoint());
            System.out.println("User id: " + newPost.getmUserid());
        }
    
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    
        }
    
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
    
        }
    
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
    
        }
    });
    
  3. 这是ReloadPoint类的代码。

    public class ReloadPoint {
    
    private String mEmail;
    private String mPoint;
    private String mUserid;
    
    public ReloadPoint(String email, String point, String userid) {
       this.mEmail = email;
       this.mPoint = point;
       this.mUserid = userid;
    
    }
    
    public String getmEmail() {
     return mEmail;
    }
    
    public void setmEmail(String mEmail) {
        this.mEmail = mEmail;
    }
    
    public String getmPoint() {
      return mPoint;
    }
    
    public void setmPoint(String mPoint) {
      this.mPoint = mPoint;
    }
    
    public String getmUserid() {
        return mUserid;
    }
    
    public void setmUserid(String mUserid) {
       this.mUserid = mUserid;
    }
    }
    
  4. 这是我要从Firebase检索的东西。 this is the child that I want to retrieve

  5. 这是Android系统,如果检索的过程成功,将显示检索到的数据。retrievedPoint.xml

  6. 日志猫的结果。Log cat result

请帮助我。

3 个答案:

答案 0 :(得分:2)

如错误消息所暗示,您的ReloadPoint类需要一个无参数的构造函数,例如:

public ReloadPoint() {

}

答案 1 :(得分:2)

请像这样在ReloadPoint类中包含无参数构造函数

public ReloadPoint() {

}

查看此问题users does not define no argument constructor

答案 2 :(得分:1)

public class ReloadPoint {

private String mEmail;
private String mPoint;
private String mUserid;

//Ni hang lupa ni. apa la sakinah
//Klau da soklan lagi, hbungi kt insta id: nyrtron
public ReloadPoint(){

}

public ReloadPoint(String email, String point, String userid) {
   this.mEmail = email;
   this.mPoint = point;
   this.mUserid = userid;

}

public String getmEmail() {
 return mEmail;
}

public void setmEmail(String mEmail) {
    this.mEmail = mEmail;
}

public String getmPoint() {
  return mPoint;
}

public void setmPoint(String mPoint) {
  this.mPoint = mPoint;
}

public String getmUserid() {
    return mUserid;
}

public void setmUserid(String mUserid) {
   this.mUserid = mUserid;
}
}

我想我已经找到你的问题了。

DatabaseReference ref = database.getReference().child("reloadPoint").child("Waniyanazahri");

更新的答案: 看起来您还没有叫课。

ref.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
      //Add this one
      ReloadPoint rp = new ReloadPoint();
      rp = dataSnapshot.getValue(ReloadPoint.class);
      System.out.println(rp);
    }
   @Override
   public void onCancelled(DatabaseError databaseError) {
   System.out.println("The read failed: " + databaseError.getCode());
   }
});