Java 自定义数组超级方法不接受列表
我创建了一个Java 自定义数组超级方法不接受列表,java,android,Java,Android,我创建了一个自定义列表适配器,我有两个构造函数,将两个不同的列表传递给构造函数的超级方法。一个超级方法获取列表,另一个super方法不获取列表,我得到了旋转的红线。悬停在上面并不能解释任何事情,附加了错误图像快照 列出适配器代码 public class ListAdapter extends ArrayAdapter<Cats> { //the list values in the List of type hero List<PlantTags> t
自定义列表适配器
,我有两个构造函数
,将两个不同的列表
传递给构造函数
的超级方法。一个超级方法获取列表,另一个super
方法不获取列表
,我得到了旋转的红线。悬停在上面并不能解释任何事情,附加了错误图像快照
列出适配器代码
public class ListAdapter extends ArrayAdapter<Cats> {
//the list values in the List of type hero
List<PlantTags> tags;
List<Cats> categories;
boolean childFragment;
int categoryId;
//activity context
Context context;
//the layout resource file for the list items
int resource;
//constructor initializing the values
public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment) {
super(context, resource, tags);
this.context = context;
this.resource = resource;
this.childFragment = childFragment;
this.tags = tags;
}
//constructor initializing the values
public ListAdapter(Context context, int resource, List<Cats> cats, boolean childFragment, int categoryId) {
super(context, resource, cats);
this.context = context;
this.resource = resource;
this.childFragment = childFragment;
this.categories = cats;
this.categoryId = categoryId;
}
//this will return the ListView Item as a View
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//we need to get the view of the xml for our list item
//And for this we need a layoutinflater
LayoutInflater layoutInflater = LayoutInflater.from(context);
//getting the view
View view = layoutInflater.inflate(resource, null, false);
//getting the view elements of the list from the view
ImageView imageView = view.findViewById(R.id.imageView);
TextView textViewName = view.findViewById(R.id.category_name);
if (!childFragment) {
//getting the hero of the specified position
Cats hero = categories.get(position);
//adding values to the list item
textViewName.setText(hero.getTitle());
} else {
//getting the hero of the specified position
PlantTags tag = tags.get(position);
//adding values to the list item
if (tag.getCategoryId() == categoryId) {
textViewName.setText(tag.getTitle());
}
}
return view;
}
}
错误
有人能解释问题出在哪里吗?您的超类构造函数必须采用四个或五个参数:
public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment)
将永远不会工作,因为您尝试调用的构造函数(一个包含三个参数)不存在。SimpleArrayAdapter
据我所知不接受两个列表。我选择了RecycleView适配器并更换了适配器,现在我可以在RecycleView的构造函数中传递任意数量的列表。有许多代码可用于此。因此,在此建议一个替代解决方案。yea@NotZack创建两个构造函数,这也是一个可能的解决方案。早些时候,我使用了一个构造函数。但我最终使用了更灵活的recycleview.:)
public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment)
public ListAdapter(Context context, int resource, List<Cats> cats, boolean childFragment, int categoryId)
super(context, resource, tags)