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

标签 java android google-cloud-firestore

This is the screenshot of my firestore structure

I want to get the data from my model stored in my firestore documents to my local variable

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());
                }
            });

请更正我的语法,我对此很陌生

This is EmployeeModel.class And please show how to pass the .getUsername to a variable String username

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 屏幕截图,请帮忙!谢谢

最佳答案

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

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

只需将 if 语句 之后的行添加到现有代码中,其余的看起来就很好。

关于java - 如何从firestore中的文档获取特定数据并将其传递给android中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58515260/

相关文章:

java - 在java中读/写一个大文件

java - 具有多个字段的 Realm java排序

android - 使用 KOTLIN 将 Firebase Firestore 中的数据显示到 RecyclerView

android - Admob 在 frameLayout 中放置在屏幕底部

Android 在其他设备上共享显示

flutter - 如何在 Firestore 中使用时间戳值查询一整天

java - 如果文档使用大型 Map 字段,则 Firebase Firestore 查询错误

java - 数据转换错误转换

java - 我如何实际使用配置了 Spring 的 JAX-RS 客户端?

java - JasperReports 比 Birt 快 10 倍吗?