I am using FragmentStatePagerAdapter to show around 5 fragments in an activity.On each activity I am showing the images which I am fetching from FirebaseListAdapter/Firebase
ArrayList<ClientData> clientData = new ArrayList<>();
int j=0;
for (String h:profileImg)
{
ClientData clientItem = new ClientData();
clientItem.setProfileImage(h);
clientItem.setName(name[j]);
clientItem.setAddress(location[j]);
clientData.add(clientItem);
j++;
}
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplication(),RecyclerView.VERTICAL,false);
recy_client.setLayoutManager(layoutManager);
recy_client.setItemAnimator(new DefaultItemAnimator());
clientAdp = new ClientAdp(getApplicationContext(),clientData);
recy_client.setAdapter(clientAdp);
Replace From
clientAdp = new ClientAdp(context,clientData);
To
clientAdp = new ClientAdp(getApplicationContext(),clientData);
in my case
I Found this way too
public class NoteAdapter extends FirestoreRecyclerAdapter<Note,NoteAdapter.NoteHolder> {
Context context;
public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull final Note model) {
context = holder.itemView.getContext();
holder.r_tv.setText(model.getTitle());
Glide.with(context).load(model.getImage()).into(holder.r_iv);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, CategoryProductActivity.class);
MainActivity.myCategory2 = model.getTitle();
MainActivity.myCategoryIcon2 = model.getImage();
context.startActivity(i);
}
});
}
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category,parent,false);
return new NoteHolder(v);
}
public static class NoteHolder extends RecyclerView.ViewHolder
{
TextView r_tv;
ImageView r_iv;
public NoteHolder(@NonNull View itemView) {
super(itemView);
r_tv = itemView.findViewById(R.id.r_tv);
r_iv = itemView.findViewById(R.id.r_iv);
}
}
}
Add Below Code on Activity on which RecyclerView is present
public static Context context;
Then in onCreate
of the same Activity on which RecyclerView is present add below Code
context = getApplicationContext();
then take reference to glide as shown below
Glide.with(YourActivity.context).load("Image Link").into(holder.YourImageView);
Your fragment is detached before response coming from firebase, So try to check getActivity()
null before using that
public void onDataChange(DataSnapshot dataSnapshot) {
if (getActivity() == null) {
return;
}
Child child = dataSnapshot.getValue(Child.class);
uname.setText(child.getUsername());
Glide.with(getActivity()).load(child.getProfileImage()).into(profileImage);
}
You can do like to check activity is null or not in other place.
private Activity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = getActivity();
}
@Override
public void onDetach() {
super.onDetach();
mActivity = null;
}
private void doAction() {
if (mActivity == null) {
return;
}
adapt = new FirebaseListAdapter<MyClassStudent>(mActivity, MyClassStudent.class, R.layout.mychild_grid_template, myRef) {
@Override
protected void populateView(final View v, MyClassStudent model, int position) {
final TextView uname = (TextView) v.findViewById(R.id.mychild_uname);
final CircleImageView profileImage = (CircleImageView) v.findViewById(R.id.mychild_image);
studRef.child(model.getUsername()).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (mActivity == null) {
return;
}
Child child = dataSnapshot.getValue(Child.class);
uname.setText(child.getUsername());
Glide.with(mActivity).load(child.getProfileImage()).into(profileImage);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
}
I prefer using isAdded() method of fragment before loading image with Glide.