使用Firebase的RecyclerAdapter - populateViewHolder在哪里?

时间:2017-12-29 18:14:11

标签: android firebase firebaseui

我正在尝试使用FirebaseRecyclerAdapter查看我的Android应用程序中的数据库信息,但是我期待populateViewHolder方法我正在让另外两个人弹出。代码如下:

FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(

                    NoteModel.class,
                    R.layout.single_note_layout,
                    NoteViewHolder.class,
                    fNotesDatabase

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

                @Override
                protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull NoteModel model) {

                }
            };

}

公共NoteViewHolder和受保护的void onBindViewHolder在使用FirebaseReccylerAdapater之后不会像过去那样预期。我在期待:

protected void populateViewHolder ( NoteViewHolder viewHolder, NoteModel model, int position) {
}

任何人都有任何想法为什么这个不再可用?我不知道如何完成代码,因为我之前没有看到过这样的代码。我需要添加....

String noteId = getRef(position).getKey();

....但我不知道在哪里。

提前致谢。

1 个答案:

答案 0 :(得分:2)

FirebaseUI migration guide是一个很好的起点。从本质上讲,你会想要做这样的事情:

FirebaseRecyclerOptions<NoteModel> options = new FirebaseRecyclerOptions.Builder<>()
        .setQuery(fNotesDatabase, NoteModel.class)
        .build();

FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> firebaseRecyclerAdapter =
        new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(options) {
            @Override
            public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new NoteViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.single_note_layout, parent, false));
            }

            @Override
            protected void onBindViewHolder(@NonNull NoteViewHolder holder,
                                            int position,
                                            @NonNull NoteModel model) {
                String key = getSnapshots().getSnapshot(position).getKey();
                holder.bind(...);
            }
        };