如何从firebase检索多个数据并在文本视图中显示它?

时间:2018-03-22 19:33:32

标签: android firebase firebase-realtime-database

我想在app中的片段中显示用户信息,我需要从firebase中检索数据。我还创建了一个单独的UserInformation java类来检索数据。

public class UserInformation {
private String username;
private String email;
private String password;
private String phoneno;
private String carplateno;

public UserInformation() {
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhoneno() {
    return phoneno;
}

public void setPhoneno(String phoneno) {
    this.phoneno = phoneno;
}

public String getCarplateno() {
    return carplateno;
}

public void setCarplateno(String carplateno) {
    this.carplateno = carplateno;
}
}

下面是我使用addEventListener的java代码。请告诉我如何提取数据并在textview中显示。

public class MyProfile extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.my_profile, null);
    final TextView t1=(TextView) v.findViewById(R.id.Usernametv);
    final TextView t2=(TextView) v.findViewById(R.id.EmailIdtv);
    final TextView t3=(TextView) v.findViewById(R.id.Passwordtv);
    final TextView t4=(TextView) v.findViewById(R.id.MobileNumbertv );
    final TextView t5=(TextView) v.findViewById(R.id.CarPlatetv);

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference("User");


    table_user.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()){
                UserInformation uinfo = new UserInformation();


            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return v;


}
}

enter image description here

1 个答案:

答案 0 :(得分:2)

for url in urls:

这样,您可以使用final DatabaseReference table_user = database.getReference("User"); table_user.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot ds : dataSnapshot.getChildren()){ String plate = ds.child("carPlateNo").getValue().toString(); String email = ds.child("email").getValue().toString(); //etc } }

在子sarah内部迭代后单独检索每个值