Firebase Recycler Adapter未填充

时间:2018-03-19 10:59:02

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

我在填充Firebase回收器适配器时遇到问题。我最终想要做的是用户搜索名称,数据库中的结果将填充recyclerView。

数据库结构如下所示:

enter image description here

要填充的代码如下所示,这都在onCreate()中:

Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");

        FirebaseRecyclerOptions<Users> options =
                new FirebaseRecyclerOptions.Builder<Users>()
                        .setQuery(mQuery, Users.class)
                        .build();

        mAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
            @NonNull
            @Override
            public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.user_single_layout, parent, false);
                return new UsersViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
                holder.setName(model.getFirstName());
            }
        };

        mUserList.setAdapter(mAdapter);

现在我只想让它填充数据库中的所有用户,但似乎没有填充。

mUserListrecyclerView

这是UsersViewHolder类:     public static class UsersViewHolder扩展了RecyclerView.ViewHolder {         查看mView;         TextView mUsername;

    UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mUsername = mView.findViewById(R.id.user_single_name);
    }

    public void setName(String name) {
        mUsername.setText(name);
    }
}

2 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");

使用:

Query mQuery = FirebaseDatabase.getInstance().getReference()
    .child("users")
    .orderByChild("first_name");

你想念一个孩子。也不要忘记开始倾听变化:

@Override
protected void onStart() {
    super.onStart();
     mAdapter.startListening();
}

并且在不需要时也停止听。

@Override
protected void onStop() {
    super.onStop();
    if( mAdapter!= null) {
         mAdapter.stopListening();
    }
}

答案 1 :(得分:0)

使用这种方式。自己遵循我的代码。

  FirebaseRecyclerOptions<Data> options=
            new FirebaseRecyclerOptions.Builder<Data>()
                    .setQuery(mIncomeDatabase,Data.class)
                    .setLifecycleOwner(this)
                    .build();

    FirebaseRecyclerAdapter<Data, MyViewHolder> firebaseRecyclelerAdapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data,parent,false));
        }

        @Override
        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull Data model) {
            holder.setName(model.getName());
            holder.setAmount(model.getAmount());
            holder.setType(model.getType());
            holder.setNote(model.getNote());
            holder.setAmount(model.getAmount());
            holder.setDate(model.getDate());
        }
    };
    recyclerView.setAdapter(firebaseRecyclelerAdapter);