我正在尝试将图像从Firebase加载到回收适配器中并显示
我尝试了两种方法来尝试解决此问题,第一段代码导致错误“您无法在尚未连接的View或Fragment上启动加载,其中getActivity()返回null(通常在getActivity返回时发生) ()在Fragment附加之前或Fragment被销毁之后被调用)“
应用程序运行的第二种方法,但是从读取其他类似问题后没有加载任何图像,我已经看过iFyreed()检查,但似乎无法正常工作,将图像上载到Firebase的名为post_images的文件夹中而与该帖子相关联的其余数据(带有发布功能的类似Instagram的应用程序)则位于firestore
public BlogRecyclerAdapter(List<BlogPost> blog_list) {
this.blog_list = blog_list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String desc_data = blog_list.get(position).getDesc();
holder.setDescText(desc_data);
String image_url = blog_list.get(position).getImage_url();
holder.setBlogImage(image_url);
}
@Override
public int getItemCount() {
return blog_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private View mView;
private TextView descView;
private ImageView blogImageView;
public Context mContext;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDescText(String descText) {
descView = mView.findViewById(R.id.blog_desc);
descView.setText(descText);
}
public void setBlogImage(final String downloadUri) {
blogImageView = mView.findViewById(R.id.Post_image_view);
Glide.with(mContext).load(downloadUri).into(blogImageView);
}}}
尝试其他方法
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
mImageStorage = FirebaseStorage.getInstance().getReference();
final String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.keepSynced(true);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
StorageReference filepath = mImageStorage.child("profile_images").child(current_uid + (".jpeg"));
Log.d("heere", "S");
// This gets the download url async
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//The download url
final String downloadUrl = uri.toString();
Log.d("tag", downloadUrl);
if (!downloadUrl.equals("default")) {
Glide.with(mContext).load(downloadUrl).into(blogImageView);
// Glide.with(getApplicationContext()).load(downloadUrl).into(mDisplayImage);
}
}});}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});}}}
我希望图像显示在“回收者”视图中
答案 0 :(得分:0)
更改此行
Glide.with(mContext).load(downloadUri).into(blogImageView);
对此:
Glide.with(itemView.getContext()).load(downloadUri).into(blogImageView);
所有ViewHolder
实例都有一个字段itemView
,该字段保证为非空,您可以从中获取上下文。
答案 1 :(得分:0)
mContext必须以这种方式在onCreateViewHolder函数中初始化
this.mContext = parent.getContext();