问题描述
我正在创建一个适配器类,我试图在其中使用模型类从 Firebase 数据库中获取数据。数据已成功上传到 firebase 数据库,但我无法从模型类中获取数据。
这是我的代码...
public class notification_model {
String message;
String userId;
public notification_model() {
}
public notification_model(String message,String userId) {
this.message = message;
this.userId = userId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
适配器类...
public class notificationAdapter extends RecyclerView.Adapter
private Context context;
private List<notification_model> notification_modelList;
public notificationAdapter(Context context,List<notification_model> notification_modelList) {
this.context = context;
this.notification_modelList = notification_modelList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.notification_item,parent,false);
return new notificationAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull notificationAdapter.ViewHolder holder,int position) {
notification_model notification_model = notification_modelList.get(position);
holder.notification_comment.setText(notification_model.getMessage());
getUserInfo(holder.notification_profile_image,holder.notification_username,notification_model.getUserId());
}
@Override
public int getItemCount() {
return notification_modelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private RoundedImageView notification_profile_image;
private ImageView notification_post_image;
private TextView notification_username,notification_comment;
public ViewHolder(@NonNull View itemView) {
super(itemView);
notification_profile_image = itemView.findViewById(R.id.notification_profile_image);
notification_post_image = itemView.findViewById(R.id.notification_post_image);
notification_username = itemView.findViewById(R.id.notification_username);
notification_comment = itemView.findViewById(R.id.notification_comment);
}
}
private void getUserInfo(RoundedImageView notification_profile_image,TextView notification_username,String publisherId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(publisherId);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
user_model user_model = snapshot.getValue(com.gratiapps.grati.Model.user_model.class);
Glide.with(notification_profile_image.getContext()).load(user_model.getProfileImage()).placeholder(R.color.grey).override(500,500).into(notification_profile_image);
notification_username.setText(user_model.getName());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void getPostimage(ImageView notification_post_image,String postId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Posts").child(postId);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
post_model post_model = snapshot.getValue(com.gratiapps.grati.Model.post_model.class);
Glide.with(notification_post_image.getContext()).load(post_model.getimageUrl()).placeholder(R.color.grey).override(500,500).into(notification_post_image);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
当我运行通知片段时,它崩溃了,并且日志显示无法在 getUserInfo 的 child() 中为参数“pathString”传递 null,当我用当前用户替换它时,消息文本视图也没有显示任何内容。 谢谢...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)