[英]How to Set Image to imageview in recycler view from Firebase using Glide?
I'm trying to set the image on the Image View as I have done the text using TextView.setText(String); 我正在尝试在图像视图上设置图像,因为我已经使用TextView.setText(String);完成了文本;
Now I need to retrieve the image from the firebase and I'm making the list of the users. 现在,我需要从Firebase检索图像,并在列出用户列表。 Now every user have an image as they have a name and email. 现在,每个用户都有一个图像,因为他们具有名称和电子邮件。
I am able to retrieve the name and email. 我可以检索姓名和电子邮件。 But the problem is that by using Glide library I exactly know how to retrieve the image, As I'm using a NON Activity Class I don't have the context. 但是问题是,通过使用Glide库,我完全知道如何检索图像,因为我使用的是NON Activity Class,所以我没有上下文。 Also I'm not able to pass it. 我也无法通过。
I want to know how can I have the context in that non activity class. 我想知道如何在非活动类中获得上下文。
This is the class in which I want to get the context to get my work done. 这是我要获取上下文以完成工作的课程。
make adapter constructor and pass context like this .. 使适配器构造函数并像这样传递上下文。
private Context context;
private List<student> student=new ArrayList();
public RecyclerViewAdaper(Context context,List<Student> studentlist){
context=context;
studnetlist=studentlist;
}
this is my adapter .... 这是我的适配器....
public class RecyclerViewAdpater extends RecyclerView.Adapter<RecyclerViewAdpater.ItemViewHolder> {
List<String> mStringList = new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;
public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
this.mStringList = mStringList;
this.mContext = mContext;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
// below code handle click event on recycler view item.
String data=mStringList.get(position);
holder.textview.setText(data);
}
@Override
public int getItemCount() {
return mStringList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.timeData);
}
}
} }
Try this 尝试这个
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ItemData[] itemsData;
private Context context;
public MyAdapter(,Context context,ItemData[] itemsData) {
this.context= context;
this.itemsData = itemsData;
}
}
Now Set adapter if its a fragment use this 现在设置适配器,如果它的片段使用此
MyAdapter mAdapter = new MyAdapter(getActivity(),itemsData);
else 其他
MyAdapter mAdapter = new MyAdapter(ActivtyClassName.this,itemsData);
// set adapter
recyclerView.setAdapter(mAdapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.