Reputation:
I am attempting to load images from firebase into a recycler adapter and display them
I have attempted two methods to try and solve this problem the first block of code resulted in the error"You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed)"
The second method the application ran but no images were loaded from reading other similar issues I have seen the isAdded() check but I couldn't seem to get that to work the images are being uploaded into a folder in firebase called post_images and the rest of the data associated with the post (Instagram like app with posting) are in 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);
}}}
Other method attempted
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) {
}
});}}}
I would like the image to be displayed in the recycler view
Upvotes: 4
Views: 9835
Reputation: 1
mContext must initialize in onCreateViewHolder function this way
this.mContext = parent.getContext();
Upvotes: 0
Reputation: 54204
Change this line
Glide.with(mContext).load(downloadUri).into(blogImageView);
to this instead:
Glide.with(itemView.getContext()).load(downloadUri).into(blogImageView);
All ViewHolder
instances have a field itemView
that is guaranteed to be non-null, and you can get the context from that.
Upvotes: 11