Good day guys,
So i have been trying to solve this problem for a few days now. The problem is that i am using fragments and a recyclerview + cardview+adapter.
I am trying to pass Context from my adapter to my fragment. I have an onClickMethod() in my Adapter class which launches a new activity from the fragment and displays a bunch of data.
The error i am getting is: error: incompatible types: AuthorsFragment cannot be converted to Context.
I know what the problem is and i am aware that an activity is in itself, a Context. But, a fragment is not. This makes it hard.
Adapter code:
public class exampleAuthorsAdapter extends RecyclerView.Adapter<exampleAuthorsAdapter.MyViewHolder> {
public Context mContext;
private ArrayList<exampleItemAuthors> mExampleListAuthors;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView authorTitle;
ImageView authorThumbnail;
CardView cardview;
public MyViewHolder(View itemView) {
super(itemView);
authorTitle = itemView.findViewById(R.id.authorCardViewTitle);
authorThumbnail = itemView.findViewById(R.id.authorCardViewImage);
cardview = itemView.findViewById(R.id.cardViewMainAuthors);
}
}
public exampleAuthorsAdapter(Context mContext, ArrayList<exampleItemAuthors> exampleItemAuthors) {
mExampleListAuthors = exampleItemAuthors;
this.mContext = mContext;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.example_item_authors, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
exampleItemAuthors currentItem = mExampleListAuthors.get(position);
final Context mContext = holder.itemView.getContext();
holder.authorTitle.setText(currentItem.getAuthorTitle());
holder.authorThumbnail.setImageResource(currentItem.getAuthorThumbnail());
holder.cardview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, activityAuthorsView.class);
intent.putExtra("Title", mExampleListAuthors.get(position).getAuthorTitle());
intent.putExtra("Description", mExampleListAuthors.get(position).getAthorDescription());
intent.putExtra("Thumbnail", mExampleListAuthors.get(position).getAuthorThumbnail());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mExampleListAuthors.size();
}
}
Fragment code:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_authors, container, false);
mLayoutManagerAuthor = new GridLayoutManager(getActivity(), 2);
return view;
}
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<exampleItemAuthors> listAuthors = new ArrayList<>();
listAuthors.add(new exampleItemAuthors("Niccolo Machiavelli", "The life
of a great human being.", R.drawable.machiavelli));
mRecyclerViewAuthor = view.findViewById(R.id.recyclerViewAuthor);
mRecyclerViewAuthor.setHasFixedSize(true);
mAdapterAuthor = new exampleAuthorsAdapter(this, getActivity());
mRecyclerViewAuthor.setLayoutManager(mLayoutManagerAuthor);
mRecyclerViewAuthor.setAdapter(mAdapterAuthor);
Any and all help would be amazing!!! I am struggling :(((((((