如何从Firestore中的文档获取特定数据并将其传递给android中的局部变量

时间:2019-10-23 03:57:29

标签: java android google-cloud-firestore

This is the screenshot of my firestore structure

  

我想从存储在我的Firestore文档中的模型中获取数据到本地变量

final EmployeeModel model = new EmployeeModel();

    firebaseFirestore.collection("USERS").document(firebaseUser.getUid()).get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if(documentSnapshot.exists()){
                        String username = documentSnapshot.getString(model.getUsername());
                        String email = documentSnapshot.getString(model.getEmail());
                        String location = documentSnapshot.getString(model.getLocation());
                        String phonenumber = documentSnapshot.getString(model.getPhonenumber());
                        String genderS = documentSnapshot.getString(model.getGender());

                        // Map<String, Object> userInfo = documentSnapshot.getData();
                        fragmentname.setText(username);
                        welcomeUser.setText(email);
                        employeeID.setText("1610225");
                        city.setText(location);
                        number.setText(phonenumber);
                        gender.setText(genderS);
                    } else {
                        Toast.makeText(getActivity(), "Document does not exist", Toast.LENGTH_SHORT).show();
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getActivity(), "Error ", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, e.toString());
                }
            });

请更正我的语法,我真的很新

  

这是EmployeeModel.class   并请说明如何将.getUsername传递给变量String用户名

public class EmployeeModel {
public static final String MFIELD_USERNAME = "username";
public static final String MFIELD_EMAIL = "email";
public static final String MFIELD_PASSWORD = "password";
public static final String MFIELD_LOCATION = "location";
public static final String MFIELD_PHONENUMBER = "phonenumber";
public static final String MFIELD_GENDER = "gender";
public static final String MFIELD_TYPE = "type";

private String username;
private String email;
private String password;
private String location;
private String phonenumber;
private String gender;
private String type;
private @ServerTimestamp Date timestamp;

public EmployeeModel() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public EmployeeModel(String username, String email, String password, String location, String phonenumber, String gender, String type, Date timestamp) {
    this.username = username;
    this.email = email;
    this.password = password;
    this.location = location;
    this.phonenumber = phonenumber;
    this.gender = gender;
    this.type = type;
    this.timestamp = timestamp;
}

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 getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getPhonenumber() {
    return phonenumber;
}

public void setPhonenumber(String phonenumber) {
    this.phonenumber = phonenumber;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Date getTimestamp() {
    return timestamp;
}

public void setTimestamp(Date timestamp) {
    this.timestamp = timestamp;
}}

这是我的EmployeeModel.class 和我的Firestore屏幕截图,请帮忙!谢谢

1 个答案:

答案 0 :(得分:0)

您缺少的是从文档中获取模型

if(documentSnapshot.exists()){
    EmployeeModel model = documentSnapshot.toObject(EmployeeModel.class);
    //

只需将if statement之后的行添加到现有代码中,其余的就可以了。