显示来自每个 uid 的所有用户信息(电子邮件、姓名等)并显示在回收者视图中
Display all user info from each uid (email, name, etc.) and display in recycler view
如何从每个用户的 firebase 数据库中获取 firstName
和 lastName
并将其显示在回收站视图中?
因此,结果将是每个用户都包含 firstName
和 lastName
的回收站视图项目。
这是我当前由 FirebaseUI 提供的 Firebase recyclerviewadapter:
public void recyclerVIewAdapter(View view) {
Firebase myFirebase = new Firebase("https://mystacks.firebaseio.com/");
AuthData authdata = myFirebase.getAuth();
Firebase ref = new Firebase("https://mystacks.firebaseio.com/").child("users");
final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.findPeopleRV);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerView.Adapter mAdapter = new FirebaseRecyclerAdapter<People, findPeopleViewHolder>(People.class, R.layout.recyclerviewlayout, findPeopleViewHolder.class, ref) {
@Override
public void populateViewHolder(findPeopleViewHolder ViewHolder,People people, int position) {
ViewHolder.fname.setText(people.getFirstName());
ViewHolder.lname.setText(people.getLastName());
}
};
recycler.setAdapter(mAdapter);
}
public static class Users {
@JsonProperty("personalProfile")
String personalProfile;
public String getPersonalProfile() {
return personalProfile;
}
public void setPersonalProfile(People people) {
personalProfile = people.getFirstName();
personalProfile = people.getLastName();
}
public Users(){
}
}
public static class People {
String firstName;
String lastName;
@JsonCreator
public People(@JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public People(){
}
}
错误代码:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@3ce9dfee; line: 1, column: 2]
我不确定您为什么要嵌套 personalProfile
属性,因为这似乎是无用的开销。请记住,您不能使用 Firebase 加载部分节点;一个节点总是完全加载或根本没有加载。
但是给定这个结构,你可以反序列化它:
public static class User {
PersonalProfile personalProfile;
public PersonalProfile getPersonalProfile() {
return personalProfile;
}
public void setPersonalProfile(PersonalProfile profile) {
personalProfile = profile;
}
}
public static class PersonalProfile {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
我强烈建议让您的 POJO 与 JSON 结构保持一致。虽然您可以使用 Jackson 进行多种类型的映射,但这只会让您的代码更难维护。一个简单的映射,就像我上面的那样,可以减少你犯错的可能性,也更容易被其他人发现。
如何从每个用户的 firebase 数据库中获取 firstName
和 lastName
并将其显示在回收站视图中?
因此,结果将是每个用户都包含 firstName
和 lastName
的回收站视图项目。
这是我当前由 FirebaseUI 提供的 Firebase recyclerviewadapter:
public void recyclerVIewAdapter(View view) {
Firebase myFirebase = new Firebase("https://mystacks.firebaseio.com/");
AuthData authdata = myFirebase.getAuth();
Firebase ref = new Firebase("https://mystacks.firebaseio.com/").child("users");
final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.findPeopleRV);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerView.Adapter mAdapter = new FirebaseRecyclerAdapter<People, findPeopleViewHolder>(People.class, R.layout.recyclerviewlayout, findPeopleViewHolder.class, ref) {
@Override
public void populateViewHolder(findPeopleViewHolder ViewHolder,People people, int position) {
ViewHolder.fname.setText(people.getFirstName());
ViewHolder.lname.setText(people.getLastName());
}
};
recycler.setAdapter(mAdapter);
}
public static class Users {
@JsonProperty("personalProfile")
String personalProfile;
public String getPersonalProfile() {
return personalProfile;
}
public void setPersonalProfile(People people) {
personalProfile = people.getFirstName();
personalProfile = people.getLastName();
}
public Users(){
}
}
public static class People {
String firstName;
String lastName;
@JsonCreator
public People(@JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public People(){
}
}
错误代码:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@3ce9dfee; line: 1, column: 2]
我不确定您为什么要嵌套 personalProfile
属性,因为这似乎是无用的开销。请记住,您不能使用 Firebase 加载部分节点;一个节点总是完全加载或根本没有加载。
但是给定这个结构,你可以反序列化它:
public static class User {
PersonalProfile personalProfile;
public PersonalProfile getPersonalProfile() {
return personalProfile;
}
public void setPersonalProfile(PersonalProfile profile) {
personalProfile = profile;
}
}
public static class PersonalProfile {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
我强烈建议让您的 POJO 与 JSON 结构保持一致。虽然您可以使用 Jackson 进行多种类型的映射,但这只会让您的代码更难维护。一个简单的映射,就像我上面的那样,可以减少你犯错的可能性,也更容易被其他人发现。