我有一个适配器
SearchAdapter.java:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
private Context mContext;
private LayoutInflater inflater;
private final List<Category> mDataset;
public SearchAdapter(Context c, List<Category> categoryList) {
mContext = c;
inflater = LayoutInflater.from(c);
mDataset= categoryList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_search_category, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.textView_category.setText(mDataset.get(position).getName());
}
static class ViewHolder extends RecyclerView.ViewHolder {
private Button button_sub;
private TextView textView_category, textView_subCategory;
public ViewHolder(View itemView) {
super(itemView);
...
}
}
Category.java:
public class Category {
private Integer id;
private String name;
private String image;
private String createdAt;
private String updatedAt;
private List<Subcategory> subcategories = new ArrayList<Subcategory>();
/**getter and setter**/
}
SubCategory.java(扩展category.java):
public class Subcategory extends Category {
private String categoryId;
/**getter and setter**/
}
我可以毫无问题地创建SearchAdapter的新实例。
mAdapter = new SearchAdapter(rootView.getContext(), categoryList);
但是,我无法将SubCategory列表传递给适配器,即使它继承自Category。
mAdapter = new SearchAdapter(rootView.getContext(), subCategoryList);
更新: 以下是我收到的错误消息:
预期参数:类别 - 实际论点:SUBCATEGORY
非常感谢任何帮助
答案 0 :(得分:1)
试试这个:
|
答案 1 :(得分:0)
由于Ashis Tiwari的建议,我找到了解决方案。
由于它是一个List,我必须按如下方式输入不同的内容:
List<Category> subCategoryList = (List<Category>)(List<?>) item.getSubcategories();