我一直在尝试用recyclerview
在我的应用程序中实现firebase ui
。我的firebase数据库中有项目,我想用firebase ui recycler view
。到目前为止,我认为我已经做得很好了,然而,当我运行我的应用程序时,它仍然显示为空白。如有任何帮助,我们将不胜感激。
public class UsersActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
query = FirebaseDatabase.getInstance().getReference().child("Users");
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions <Users> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(query, Users.class).build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(firebaseRecyclerOptions){
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_single_layout, parent,false);
return new UsersViewHolder(mView);
}
@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
holder.setName(model.getName());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName (String name){
TextView mName = mView.findViewById(R.id.user_name);
mName.setText(name);
}
}
}
您需要添加以下内容:
FirebaseRecyclerAdapter
使用事件侦听器监视对Firebase查询的更改。要开始侦听数据,请调用startListening()
方法。您可能需要在onStart()
方法中调用此函数。在调用startListening()
之前,请确保已完成读取数据所需的任何身份验证,否则查询将失败。
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}
更多信息点击这里:
https://github.com/firebase/FirebaseUI-Android/tree/master/database