为什么我不能使用populateViewHolder覆盖方法?

时间:2017-11-18 09:31:37

标签: java android firebase firebase-realtime-database firebaseui

我尝试使用我在{strong> FirebaseRecylerAdapter 中使用的database fragment方法从onViewCreated检索数据

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
    }

    public static class AllUsersViewHolder extends RecyclerView.ViewHolder {

        View view;

        public AllUsersViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }
    }

当我使用firebaseRecyclerAdapter = new FirebaseRecyclerAdapter时,它不会给我 populateViewHolder 方法,只需给出如下方法

FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
            @Override
            protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {

            }

            @Override
            public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
            }
        };

有人可以告诉我为什么会这样吗

1 个答案:

答案 0 :(得分:8)

Firebase...Adapter类的构造函数在FirebaseUI版本3中已更改。鉴于此查询:

Query query = FirebaseDatabase.getInstance()
    .getReference()
    .child("users")
    .equalTo(name);

如果您使用的是版本3或更高版本,请使用FirebaseRecyclerOptions

 FirebaseRecyclerOptions<model_class_name> options =
            new FirebaseRecyclerOptions.Builder<model_class_name>()
                    .setQuery(query, model_class_name.class)
                    .build();

然后声明一个FirebaseRecyclerAdapter:

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {

如上所述添加变量options。它是类FirebaseRecyclerOptions

的变量

然后添加你必须使用的项目onBindViewHolder,因为它是最新版本的firebase ui:

 @Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
    // Bind the class object to the holder
    // ...
}

有关详情,请查看:https://github.com/firebase/FirebaseUI-Android/tree/master/database