你好!一开始,我必须说,我已经显示了许多有关Firebase的文档,并花了很多时间在堆栈上的现有主题上。
让我们开始。我正在为使用聊天(称为响应(的汽车用户创建应用程序,包括Firebase实现。现在,我正在努力使用RecyClerview展示用户列表。在Firebase中,我有数据库"用户"。使用我的代码后,我没有任何用户的空白活动。
在'com.firebaseui中:firebase-ui-database:2.4.0'(与populateViewHolder(我的activitivivitive效果很好。有人知道我的代码怎么了吗?感谢您的任何答复:(
这是我的活动:
public class ResponseAllUsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView allUsersList;
private DatabaseReference allDatabaseUserreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_response_all_users);
mToolbar = (Toolbar) findViewById(R.id.all_users_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
allUsersList = (RecyclerView) findViewById(R.id.all_users_list);
allUsersList.setHasFixedSize(true);
allUsersList.setLayoutManager(new LinearLayoutManager(this));
allDatabaseUserreference = FirebaseDatabase.getInstance().getReference().child("Users");
}
@Override
protected void onStart() {
super.onStart();
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Users");
FirebaseRecyclerOptions<Response_AllUsers> options =
new FirebaseRecyclerOptions.Builder<Response_AllUsers>()
.setQuery(query, Response_AllUsers.class)
.build();
FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder>
(options)
{
@Override
protected void onBindViewHolder(@NonNull AllUsersViewHolder holder, int position, @NonNull Response_AllUsers model)
{
holder.setUser_name(model.getUser_name());
holder.setUser_status(model.getUser_status());
holder.setUser_image(getApplicationContext(), model.getUser_image());
}
@NonNull
@Override
public AllUsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.response_users_display_layout,parent,false);
return new AllUsersViewHolder(view);
}
};
allUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public AllUsersViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setUser_name(String user_name)
{
TextView name = (TextView) mView.findViewById(R.id.all_users_username);
name.setText(user_name);
}
public void setUser_status(String user_status)
{
TextView status = (TextView) mView.findViewById(R.id.all_users_status);
status.setText(user_status);
}
public void setUser_image(Context ctx, String user_image)
{
CircleImageView image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);
Picasso.with(ctx).load(user_image).into(image);
}
}
}
response_allusers:
public class Response_AllUsers {
public String user_name;
public String user_image;
public String user_status;
public Response_AllUsers()
{
}
public Response_AllUsers(String user_name, String user_image, String user_status) {
this.user_name = user_name;
this.user_image = user_image;
this.user_status = user_status;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_image() {
return user_image;
}
public void setUser_image(String user_image) {
this.user_image = user_image;
}
public String getUser_status() {
return user_status;
}
public void setUser_status(String user_status) {
this.user_status = user_status;
}
}
您使用的是新版本的firebaseui,然后在onStart()
中您需要添加以下内容:
firebaseRecyclerAdapter.startListening();
开始收听数据
和内部onStop()
,添加:
firebaseRecyclerAdapter.stopListening();
更多信息在这里:
firebase UI生命周期