Firebase - 从数据库中检索用户信息

时间:2017-09-29 07:38:40

标签: android firebase firebase-realtime-database

enter image description here>我有3节课。

  • DriverDatabase.class
  • AddInfoDriver.class
  • DriverProfile.class

DriverDatabase.class我设置了(fName,lName ....和其他字段。)。另外,将{get; and set;}放在那些属性上。

AddInfoDriver.class我引用了FirebaseDatabaseDriverDatabase.class,这样,只要用户填写这些edittexts,它就会保存到Firebase数据库。

问题是:我无法检索下面DriverProfile.class中的5个Edittext值是用于检索的代码段。

public class DriverProfile extends Fragment {

@BindView(R.id.btn_save) Button btnsave;
@BindView(R.id.edtx_DoB) EditText edtxdob;
@BindView(R.id.edtx_fname) EditText edtx_fname;
@BindView(R.id.edtx_lname) EditText edtx_lname;
@BindView(R.id.spinner_type) Spinner spn_type;
@BindView(R.id.edtx_number) EditText edtx_number;

private DatabaseReference driverfname, driverlname;


public DriverProfile() {
//Constructed is required

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my_profile, container, false);
    ButterKnife.bind(this, view);

    driverfname = FirebaseDatabase.getInstance().getReference().child("Drivers").child("fname");
    driverlname  = FirebaseDatabase.getInstance().getReference().child("Drivers").child("lname");

    driverfname.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            edtx_fname.setText(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Didnt编写所有edittext代码,只需查看我的ValueEventListener以获取驱动程序的fname。当驱动程序在child("Drivers")

中保存时,它不显示数据

1 个答案:

答案 0 :(得分:1)

要获取所有驱动程序的值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference driversRef = rootRef.child("Drivers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String cardNumber = ds.child("cardNumber").getValue(String.class);
            String cardType = ds.child("cardType").getValue(String.class);
            String dateOfBirth = ds.child("dateOfBirth").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            String surname = ds.child("surname").getValue(String.class);
            Log.d("TAG", cardNumber + " / " + cardType + " / " + dateOfBirth + " / " + name + " / " + surname);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
driversRef.addListenerForSingleValueEvent(eventListener);