我正在使用Glide插入通过片段调用的适配器从Firestore查询的图像。
我完全不知道我应该作为 Context 通过什么。我已经尝试过getActivity()来获取父活动,即调用片段getApplicationContext()的getContext()。当调用适配器无济于事时,我什至尝试从片段传递上下文。我不知道该如何解决。
除了这些,我试图从viewGroup和itemView中获取上下文,因为它们不会为空,但最终出现如下错误:
您不能在尚未连接的View或其中getActivity()返回null的Fragment上开始加载(通常在附着Fragment之前或销毁Fragment之后调用getActivity()时发生)。
请帮助。
这是我的适配器:
public class RestaurantAdapter extends FirestoreRecyclerAdapter<RestaurantItems, RestaurantAdapter.RestaurantHolder> {
ImageView restaurantLogo;
public RestaurantAdapter(@NonNull FirestoreRecyclerOptions<RestaurantItems> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull RestaurantHolder holder, int position, @NonNull RestaurantItems model) {
holder.tvRestaurantName.setText(model.getRestaurantName());
holder.tvRestaurantAddress.setText(model.getRestaurantAddress());
holder.tvRestaurantDescription.setText(model.getRestaurantDescription());
Glide.with().load(model.getRestaurantLogo()).placeholder(R.drawable.restaurant_default).into(restaurantLogo);
}
@NonNull
@Override
public RestaurantHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.restaurant_card, viewGroup, false);
return new RestaurantHolder(view);
}
class RestaurantHolder extends RecyclerView.ViewHolder {
TextView tvRestaurantName, tvRestaurantAddress, tvRestaurantDescription;
ImageView tvRestaurantLogo;
public RestaurantHolder(@NonNull View itemView) {
super(itemView);
tvRestaurantName = itemView.findViewById(R.id.restaurant_name);
tvRestaurantAddress = itemView.findViewById(R.id.restaurant_address);
tvRestaurantDescription = itemView.findViewById(R.id.restaurant_description);
tvRestaurantLogo = itemView.findViewById(R.id.restaurant_logo);
}
}
}