英文:
Custom array super method not accepting the list
问题
我创建了一个自定义的列表适配器,有两个构造函数,分别传递了两个不同的列表给构造函数的super
方法。一个super
方法接受列表,另一个super
方法不接受列表,我得到了一个红色波浪线。悬停在上面没有解释任何内容,错误图像快照已附上。
列表适配器代码:
public class ListAdapter extends ArrayAdapter<Cats> {
// 列表中的值,类型为PlantTags的列表
List<PlantTags> tags;
List<Cats> categories;
boolean childFragment;
int categoryId;
// 上下文
Context context;
// 列表项的布局资源文件
int resource;
// 初始化值的构造函数
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;
}
// 初始化值的构造函数
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;
}
// 返回ListView项作为View
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// 我们需要获取列表项的xml视图
// 为此,我们需要一个LayoutInflater
LayoutInflater layoutInflater = LayoutInflater.from(context);
// 获取视图
View view = layoutInflater.inflate(resource, null, false);
// 从视图中获取列表项的视图元素
ImageView imageView = view.findViewById(R.id.imageView);
TextView textViewName = view.findViewById(R.id.category_name);
if (!childFragment) {
// 获取指定位置的Cats对象
Cats hero = categories.get(position);
// 将值添加到列表项
textViewName.setText(hero.getTitle());
} else {
// 获取指定位置的PlantTags对象
PlantTags tag = tags.get(position);
// 将值添加到列表项
if (tag.getCategoryId() == categoryId) {
textViewName.setText(tag.getTitle());
}
}
return view;
}
}
PlantTags的POJO模型:
public class PlantTags {
@Expose
@SerializedName("Title")
private String Title;
@Expose
@SerializedName("CategoryId")
private int CategoryId;
@Expose
@SerializedName("Id")
private int Id;
@Expose
@SerializedName("$id")
private String $id;
public String getTitle() {
return Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
public int getCategoryId() {
return CategoryId;
}
public void setCategoryId(int CategoryId) {
this.CategoryId = CategoryId;
}
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String get$id() {
return $id;
}
public void set$id(String $id) {
this.$id = $id;
}
}
有人可以解释问题出在哪里吗?
1: https://i.stack.imgur.com/B2Y4W.png
英文:
I have created a custom list adapter
, i have two constructors
, passing two different lists
to super
methods of constructor
. One super method taking list, the other super
method doesn't take the list
, i am getting swiggly red line. Hovering on it doesn't explain anything, error image snapshot is attached.
List adapter code
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;
}
}
** Pojo model for PlantTags **
public class PlantTags {
@Expose
@SerializedName("Title")
private String Title;
@Expose
@SerializedName("CategoryId")
private int CategoryId;
@Expose
@SerializedName("Id")
private int Id;
@Expose
@SerializedName("$id")
private String $id;
public String getTitle() {
return Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
public int getCategoryId() {
return CategoryId;
}
public void setCategoryId(int CategoryId) {
this.CategoryId = CategoryId;
}
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String get$id() {
return $id;
}
public void set$id(String $id) {
this.$id = $id;
}
}
can anyone explain where the problem is?
答案1
得分: 1
你的超类构造函数必须接受四个或五个参数之一:
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)
永远不会起作用,因为你试图调用的构造函数(带有三个参数的那个)不存在。
英文:
Your superclass constructor has to take either four or five parameters being:
public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment)
or
public ListAdapter(Context context, int resource, List<Cats> cats, boolean childFragment, int categoryId)
You need to fill all four-five parameters or create a new constructor with less.
super(context, resource, tags)
Will never work since the constructor that you are trying to call (one with three parameters) does not exist.
答案2
得分: 0
简单的ArrayAdapter
据我所知不能接受两个列表。我选择了RecycleView
适配器,并且替换了适配器,现在我可以在RecycleView
的构造函数中传递任意多的列表。关于这方面有很多代码可用。因此,在这里只是提供一个备选方案。
英文:
Simple ArrayAdapter
does not accept two lists as far as i know. I have opted for RecycleView adapter and replaced adapter and i can now pass as many lists as i want in the constructor
of recycleview. There are many code available for that. So just suggestion an alternate solution here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论