如何使用FirebaseRecyclerAdapter

时间:2018-03-31 06:58:10

标签: android firebase firebase-realtime-database firebaseui

@Override
public void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<AllUsers, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>() {

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

        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull AllUsers model) {
            holder.setName(model.getName());
            holder.setStatus(model.getStatus());
        }

    };
    mUsersList.setAdapter(firebaseRecyclerAdapter);
}

public static class UsersViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setName(String name) {
        TextView mDisplayName = (TextView) mView.findViewById(R.id.display_name);
        mDisplayName.setText(name);
    }
    public void setStatus(String status) {
        TextView mDisplayStatus = (TextView) mView.findViewById(R.id.display_status);
        mDisplayStatus.setText(status);
    }
}

错误位于以下行中,其中显示了()下的红线:         FirebaseRecyclerAdapter<AllUsers, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>() {

里面的参数应该是什么?

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query query=ref.child("Users").orderByChild("name").equalTo(name);
FirebaseRecyclerOptions<AllUsers> options =
    new FirebaseRecyclerOptions.Builder<AllUsers>()
            .setQuery(query, AllUsers.class)
            .build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>(options) {

首先,您需要通过构建FirebaseRecyclerOptions

来配置适配器

AllUsers是您的POJO类,您需要在回收器适配器中进行查询。

更多信息:

https://github.com/firebase/FirebaseUI-Android/tree/master/database