显示每个uid的所有用户信息(电子邮件,姓名等)并显示在回收站视图中

时间:2016-02-07 18:43:35

标签: android firebase firebase-realtime-database

enter image description here

我如何从每个用户的firebase数据库中获取firstNamelastName并将其显示在回收站视图上?

因此,结果将是每个用户都包含firstNamelastName的回收者视图项。

这是我目前使用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]

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要嵌套一个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进行多种类型的映射,但它只会使您的代码难以维护。一个简单的映射,就像我上面所做的那样,可以减少你犯错误的可能性,让其他人更容易接受错误。