从Firebase获取数据并绑定到回收者视图

时间:2020-07-02 14:52:22

标签: firebase android-studio firebase-realtime-database

首先使用此版本的数据库的即时消息:实现'com.firebaseui:firebase-ui-database:6.2.1' 我正在尝试此代码并在粗体语句错误处出错:预期为1个参数,但找到了4个。 代码是: 私人void loadMenu(){

    FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter=new FirebaseRecyclerAdapter<Category, MenuViewHolder>**(Category.class,R.layout.menu_item,MenuViewHolder.class,category)** {
        @Override
        protected void onBindViewHolder(@NonNull MenuViewHolder holder, int position, @NonNull Category model) {


        }

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

编辑者之一: 私人void loadMenu(){

        FirebaseRecyclerOptions<Category> options=new FirebaseRecyclerOptions.Builder<Category>()
                .setQuery(category,Category.class)
                .build();

    FirebaseRecyclerAdapter adapter= new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
        @NonNull
        @Override
        public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView= LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.menu_item,parent,false);
            return   new MenuViewHolder(itemView);
        }

        @Override
        protected void onBindViewHolder(@NonNull MenuViewHolder viewHolder, int position, @NonNull Category model) {
            viewHolder.txtMenuName.setText(model.getName());
            Picasso.with(getBaseContext()).load(model.getImage())
                    .into(viewHolder.imageView);
            final Category clickItem=model;
            viewHolder.setItemClickListener...

            }


        }
    };
}

公共类MenuViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener {

public TextView txtMenuName;
public ImageView imageView;
private ItemClickListener itemClickListener;

public MenuViewHolder(@NonNull View itemView) {
    super(itemView);

    txtMenuName=(TextView)itemView.findViewById(R.id.menu_name);
    imageView= (ImageView)itemView.findViewById(R.id.menu_image);

}

@Override
public void onClick(View view) {

}

}

1 个答案:

答案 0 :(得分:0)

FirebaseUI的最新版本(3.x)与以前的版本相比,实现了初始化FirebaseRecyclerAdapter的不同方法。通过使用FirebaseRecyclerAdapter文档:

首先,通过构建FirebaseRecyclerOptions配置适配器。在这种情况下,我们将继续我们的聊天示例:

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

接下来创建FirebaseRecyclerAdapter对象。您应该已经有一个ViewHolder子类来显示每个项目。

因此,对于您的示例,您需要执行以下操作:

FirebaseRecyclerOptions<image_details> options =
        new FirebaseRecyclerOptions.Builder<image_details>()
                .setQuery(myRef, image_details.class)
                .build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(options) {
    @Override
    public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.individual_row, parent, false);

        return new BlogViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
        // Bind the image_details object to the BlogViewHolder
        // ...
    }
};

最后,FirebaseRecyclerAdapter使用事件侦听器监视对Firebase查询的更改。要开始侦听数据,请调用adapter.startListening()和adapter.stopListening()停止侦听器。建议分别在onStart()和onStop()中调用它们。有关详细信息,请参阅FirebaseRecyclerAdapter生命周期文档。摘自:firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter