0

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 :(((((((

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Vanguard
  • 13
  • 11

1 Answers1

1

This is your adapter constructor:

public exampleAuthorsAdapter(Context mContext, ArrayList<exampleItemAuthors> exampleItemAuthors) {

this.mExampleListAuthors = exampleItemAuthors;
this.mContext = mContext;

}

And here you creates a new adapter in your fragment:

 mAdapterAuthor = new exampleAuthorsAdapter(this, getActivity()); //won't work

As we see your 1st argument must be the context (getActivity) and the 2nd is the data ( ArrayList listAuthors), so change the adapter initialisation raw like this:

 mAdapterAuthor = new exampleAuthorsAdapter(getActivity(), listAuthors);

Enjoy!

  • I tried this but it didnt work before. Now it does as i have a constructor in my adapter class. But the new activity does not display the data that is meant to be passed from the onClickMethod()...am i missing something? – Vanguard Mar 10 '19 at 23:03
  • I was edited this answer but he declined. Update this line "mExampleListAuthors = exampleItemAuthors;" with "this.mExampleListAuthors = exampleItemAuthors;" – twenk11k Mar 10 '19 at 23:08
  • without doing this mExampleListAuthors will not set via constructor – twenk11k Mar 10 '19 at 23:09
  • yes, I missed that ))) – Alexander Gapanowich Mar 10 '19 at 23:12