需要获取文本&来自FireBase数据库的图像并使用FireBase RecyclerAdapter查看,但问题是:
FirebaseRecyclerAdapter<Category, MenuViewHolder>
下面有一条红线,上面写着
类"从FirebaseRecyclerAdapter派生的匿名类"必须声明为抽象类,或在"FirebaseReccyclerAdapter"中实现抽象方法"onBindViewHolder(VH,int,T(">
- populateViewHolder我手动写的,我认为这种方法已经折旧了
点击查看显示问题的图片
private void loadMenu() {
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder> (Category.class , R.layout.menu_item , MenuViewHolder.class , category){
@Override
protected void populateViewHolder(@NonNull MenuViewHolder viewHolder, Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.ImageView);
Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(Home.this, "" + clickItem.getName(), Toast.LENGTH_SHORT).show();
}
});
}
@NonNull
@Override
public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
};
recycler_menu.setAdapter(adapter);
}
非常感谢
您应该覆盖onBindViewHolder
,以便它可以调用populateViewHolder
。试试这样的东西:
@Override
public void onBindViewHolder(VH viewHolder, int position) {
Category model = getItem(position);
populateViewHolder(viewHolder, model, position);
}
此外,我认为在onCreateViewHolder
中,您应该扩大项目布局并返回一个新的ViewHolder。
要使用FirebaseUI中的适配器,需要覆盖它们并实现onCreateViewHolder
和onBindViewHolder
。但是不需要populateViewHolder
方法。
从FirebaseUI关于使用FirebaseRecyclerAdapter的文档中可以看到一个最小的例子:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
@Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message, parent, false);
return new ChatHolder(view);
}
@Override
protected void onBindViewHolder(ChatHolder holder, int position, Chat model) {
// Bind the Chat object to the ChatHolder
// ...
}
};
现在populateViewHolder
中的大多数代码在使用FirebaseUI时都会出现在onBindViewHolder
中。