无法从模型类获取数据

我正在创建一个适配器类,我试图在其中使用模型类从 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,当我用当前用户替换它时,消息文本视图也没有显示任何内容。 谢谢...

poruin 回答:无法从模型类获取数据

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/6241.html

大家都在问