如何从文档中检索嵌套对象并将其显示在FirestoreRecyclerOptions中?

时间:2019-08-16 17:07:12

标签: java android firebase google-cloud-firestore firebaseui

我在Firestore中具有此数据结构,如图所示。

My Data Structure in firestore

我正在使用FirestoreRecyclerOptions显示用户的地址。我将地址存储为POJO(UserAddressModel.java),我不知道这是否是最佳实践,还是我不知道如何存储。

UserInformationsModel.java

   public class UserInformationsModel {

private String email, username, name ;
private UserAddressModel userAddress;   
private String phoneNumber;
private String photo;
private String photoUrl;



public UserInformationsModel() {
}
}

UserAddressModel.java

 public class UserAddressModel {

private String fullName, streetAddressOne, streetAddressTwo, city, state, country;
private String zipCode;

public UserAddressModel() {
}



public UserAddressModel(  String streetAddressOne, String city, String state, String country, String zipCode) {

    this.streetAddressOne = streetAddressOne;
    this.city = city;
    this.state = state;
    this.country = country;
    this.zipCode = zipCode;
}
public String getFullName() {
    return fullName;
}

public String getStreetAddressOne() {
    return streetAddressOne;
}

public String getStreetAddressTwo() {
    return streetAddressTwo;
}

public String getCity() {
    return city;
}

public String getState() {
    return state;
}

public String getCountry() {
    return country;
}

public String getZipCode() {
    return zipCode;
}

我已经创建了AdapterAddress类,该类扩展了FirestoreRecyclerAdapter及其布局。

我的问题是如何在recyclerview中显示address1和address2? 因为我注意到setQuery方法需要查询,但是我不知道如何获取地址

Query query = coll_ref.orderBy("?????");

  FirestoreRecyclerOptions<UserAddressModel> options = new   FirestoreRecyclerOptions.Builder<UserAddressModel>()
            .setQuery(query, UserAddressModel.class)
            .build();
     adapterAddress = new AdapterAddress(options);

1 个答案:

答案 0 :(得分:1)

您无法使用查询获取那些地址。当您需要获取集合中的所有文档或需要对其进行过滤时,forward method()对象需要将FirestoreRecyclerOptionsQuery传递给CollectionReference方法。 / p>

您需要做的是在用户文档上获取引用并进行setQuery()调用:

get()

请参见,String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); CollectionReference usersRef = rootRef.collection("users"); usersRef.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document.exists()) { Map<String, Object> map = document.getData(); } } } }); 返回一张地图。只需遍历地图并获取地址即可。