无法在 Recyclerview 中显示图像 - 文本已成功显示
Posted
技术标签:
【中文标题】无法在 Recyclerview 中显示图像 - 文本已成功显示【英文标题】:Unable to Get Images to Display in Recycler View- Text is Sucessfully Displayed 【发布时间】:2021-04-28 16:59:12 【问题描述】:请看以下问题:
RecyclerView 中未成功显示所有用户配置文件图像。 文本视图已成功显示在 RecyclerView 中。 我在网上查找了其他类似问题,但未能找到解决方案。 我已更新我的 google play 服务和我的 Firebase 存储依赖项。 我能够在另一个活动中成功提取当前用户个人资料图像。 我收到以下 404 错误消息:**Adapter**
public class FindFriendsAdapter extends RecyclerView.Adapter<FindFriendsAdapter.FindFriendsViewHolder>
private Context context;
private List<FindFriendModel> findFriendModelList;
////This is a constructor and is a must have for recyclerviews/////
public FindFriendsAdapter(Context context, List<FindFriendModel> findFriendModelList)
this.context = context;
this.findFriendModelList = findFriendModelList;
////This is a constructor and is a must have for recyclerviews/////
@NonNull
@Override
public FindFriendsAdapter.FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(context).inflate(R.layout.find_friends_layout, parent, false);
return new FindFriendsViewHolder(view); ///// layout converts our xml layout file to a programmable file some kind of way.
@Override
public void onBindViewHolder(@NonNull FindFriendsAdapter.FindFriendsViewHolder holder, int position)
final FindFriendModel friendModel = findFriendModelList.get(position);
holder.text_view_full_name.setText(friendModel.getFull_name());
StorageReference fileref = FirebaseStorage.getInstance().getReference().child(Constants.IMAGES_FOLDER +"/" + friendModel.getProfileimage());
fileref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
@Override
public void onSuccess(Uri uri)
Glide.with(context)
.load(uri)
.placeholder(R.drawable.small_grey_circle)
.error(R.drawable.small_grey_circle)
.into(holder.imageView_profile);
);
@Override
public int getItemCount()
return findFriendModelList.size();
public class FindFriendsViewHolder extends RecyclerView.ViewHolder
private CircleImageView imageView_profile;
private TextView text_view_full_name;
private ImageButton button_request, button_cancel_request;
public FindFriendsViewHolder(@NonNull View itemView)
super(itemView);
imageView_profile = itemView.findViewById(R.id.find_friends_profile_picture);
text_view_full_name = itemView.findViewById(R.id.find_friends_user_full_name);
button_request = itemView.findViewById(R.id.button_send_requests);
button_cancel_request = itemView.findViewById(R.id.button_cancel_requests);
**Model Class**
public class FindFriendModel
private String full_name;
private String profileimage;
private String userID;
private boolean requestSent;
public FindFriendModel(String full_name, String profileimage, String userID, boolean requestSent)
this.full_name= full_name;
this.profileimage = profileimage;
this.userID = userID;
this.requestSent = requestSent;
public FindFriendModel()
public String getFull_name()
return full_name;
public void setFull_name(String full_name)
this.full_name = full_name;
public String getProfileimage()
return profileimage;
public void setProfileimage(String profileimage)
this.profileimage = profileimage;
public String getUserID()
return userID;
public void setUserID(String userID)
this.userID = userID;
public boolean isRequestSent()
return requestSent;
public void setRequestSent(boolean requestSent)
this.requestSent = requestSent;
**Fragment Java Class**
public class FindFriendsFragment extends Fragment
private RecyclerView recycler_view_find_friends;
private FindFriendsAdapter findFriendsAdapter;
private List<FindFriendModel> findFriendModelList;
private TextView text_view_empty_Friends_List;
private DatabaseReference databaseReference;
private FirebaseUser currentUser;
public FindFriendsFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find_friends, container, false);
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
recycler_view_find_friends = view.findViewById(R.id.recycler_view_find_friends);
text_view_empty_Friends_List = view.findViewById(R.id.text_view_empty_find_friends);
recycler_view_find_friends.setLayoutManager(new LinearLayoutManager(getActivity()));
findFriendModelList = new ArrayList<>();
findFriendsAdapter = new FindFriendsAdapter(getActivity(), findFriendModelList);
recycler_view_find_friends.setAdapter(findFriendsAdapter);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
currentUser = FirebaseAuth.getInstance().getCurrentUser();
text_view_empty_Friends_List.setVisibility(View.VISIBLE);
Query query = databaseReference.orderByChild("full_name");
query.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
findFriendModelList.clear();
for (final DataSnapshot ds : dataSnapshot.getChildren())
final String userID = ds.getKey();
if (userID.equals(currentUser.getUid()))
return;
if (ds.child("full_name").getValue()!=null)
String fullName = ds.child("full_name").getValue().toString();
String profileImage = ds.child("profileimage").getValue().toString();
findFriendModelList.add(new FindFriendModel(fullName, profileImage, userID, false));
findFriendsAdapter.notifyDataSetChanged();
text_view_empty_Friends_List.setVisibility(View.GONE);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
Toast.makeText(getContext(),"Failed to fetch friends", Toast.LENGTH_SHORT).show();
);
````
【问题讨论】:
【参考方案1】:ds.child("full_name").getValue()!=null
应替换为
Objects.equals(ds.child("full_name"), null)
当我遇到同样的错误时它对我有用,也许它也会帮助你!
【讨论】:
我仍然收到错误,但感谢您的尝试。【参考方案2】:发现问题: 问题是我已经将我的图像图像保存到 Firebase 存储,并且我试图再次获取下载 url 字符串。 我修复了适配器的以下部分:
@Override
public void onBindViewHolder(@NonNull FindFriendsAdapter.FindFriendsViewHolder holder, int position)
final FindFriendModel friendModel = findFriendModelList.get(position);
holder.text_view_full_name.setText(friendModel.getFull_name());
Glide.with(context)
.load(friendModel.getProfileimage())
.placeholder(R.drawable.small_grey_circle)
.error(R.drawable.small_grey_circle)
.into(holder.imageView_profile);
【讨论】:
以上是关于无法在 Recyclerview 中显示图像 - 文本已成功显示的主要内容,如果未能解决你的问题,请参考以下文章
无法将数据从RecyclerView适配器传递到Viewpager适配器
如何在 Recyclerview 中显示图像 - Recyclerview 图像显示错误