伙计们,
因此,我几天来一直在尝试解决此问题。 问题是我正在使用片段和一个recyclerview + cardview + adapter。
我试图将Context从我的适配器传递到我的片段。我的Adapter类中有一个onClickMethod(),它从片段中启动一个新活动并显示大量数据。
我遇到的错误是: 错误:类型不兼容:AuthorsFragment无法转换为Context 。
我知道问题出在哪里,我知道活动本身就是上下文。但是,片段不是。这很难。
适配器代码:
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();
}
}
片段代码:
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);
任何和所有帮助都将是惊人的!!!我在挣扎:((((((((>>
答案 0 :(得分:1)
这是您的适配器构造函数:
public exampleAuthorsAdapter(Context mContext, ArrayList<exampleItemAuthors> exampleItemAuthors) {
this.mExampleListAuthors = exampleItemAuthors;
this.mContext = mContext;
}
在这里,您将在片段中创建一个新适配器:
mAdapterAuthor = new exampleAuthorsAdapter(this, getActivity()); //won't work
我们看到您的第一个参数必须是上下文(getActivity),第二个参数是数据(ArrayList listAuthors),因此,请像这样更改适配器的初始化原始数据:
mAdapterAuthor = new exampleAuthorsAdapter(getActivity(), listAuthors);
享受!