自定义数组超级方法不接受该列表。

huangapple go评论146阅读模式
英文:

Custom array super method not accepting the list

问题

我创建了一个自定义的列表适配器,有两个构造函数,分别传递了两个不同的列表给构造函数的super方法。一个super方法接受列表,另一个super方法不接受列表,我得到了一个红色波浪线。悬停在上面没有解释任何内容,错误图像快照已附上。

列表适配器代码:

  1. public class ListAdapter extends ArrayAdapter<Cats> {
  2. // 列表中的值,类型为PlantTags的列表
  3. List<PlantTags> tags;
  4. List<Cats> categories;
  5. boolean childFragment;
  6. int categoryId;
  7. // 上下文
  8. Context context;
  9. // 列表项的布局资源文件
  10. int resource;
  11. // 初始化值的构造函数
  12. public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment) {
  13. super(context, resource, tags);
  14. this.context = context;
  15. this.resource = resource;
  16. this.childFragment = childFragment;
  17. this.tags = tags;
  18. }
  19. // 初始化值的构造函数
  20. public ListAdapter(Context context, int resource, List<Cats> cats, boolean childFragment, int categoryId) {
  21. super(context, resource, cats);
  22. this.context = context;
  23. this.resource = resource;
  24. this.childFragment = childFragment;
  25. this.categories = cats;
  26. this.categoryId = categoryId;
  27. }
  28. // 返回ListView项作为View
  29. @NonNull
  30. @Override
  31. public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  32. // 我们需要获取列表项的xml视图
  33. // 为此,我们需要一个LayoutInflater
  34. LayoutInflater layoutInflater = LayoutInflater.from(context);
  35. // 获取视图
  36. View view = layoutInflater.inflate(resource, null, false);
  37. // 从视图中获取列表项的视图元素
  38. ImageView imageView = view.findViewById(R.id.imageView);
  39. TextView textViewName = view.findViewById(R.id.category_name);
  40. if (!childFragment) {
  41. // 获取指定位置的Cats对象
  42. Cats hero = categories.get(position);
  43. // 将值添加到列表项
  44. textViewName.setText(hero.getTitle());
  45. } else {
  46. // 获取指定位置的PlantTags对象
  47. PlantTags tag = tags.get(position);
  48. // 将值添加到列表项
  49. if (tag.getCategoryId() == categoryId) {
  50. textViewName.setText(tag.getTitle());
  51. }
  52. }
  53. return view;
  54. }
  55. }

PlantTags的POJO模型:

  1. public class PlantTags {
  2. @Expose
  3. @SerializedName("Title")
  4. private String Title;
  5. @Expose
  6. @SerializedName("CategoryId")
  7. private int CategoryId;
  8. @Expose
  9. @SerializedName("Id")
  10. private int Id;
  11. @Expose
  12. @SerializedName("$id")
  13. private String $id;
  14. public String getTitle() {
  15. return Title;
  16. }
  17. public void setTitle(String Title) {
  18. this.Title = Title;
  19. }
  20. public int getCategoryId() {
  21. return CategoryId;
  22. }
  23. public void setCategoryId(int CategoryId) {
  24. this.CategoryId = CategoryId;
  25. }
  26. public int getId() {
  27. return Id;
  28. }
  29. public void setId(int Id) {
  30. this.Id = Id;
  31. }
  32. public String get$id() {
  33. return $id;
  34. }
  35. public void set$id(String $id) {
  36. this.$id = $id;
  37. }
  38. }

错误:
自定义数组超级方法不接受该列表。

有人可以解释问题出在哪里吗?
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

  1. public class ListAdapter extends ArrayAdapter&lt;Cats&gt; {
  2. //the list values in the List of type hero
  3. List&lt;PlantTags&gt; tags;
  4. List&lt;Cats&gt; categories;
  5. boolean childFragment;
  6. int categoryId;
  7. //activity context
  8. Context context;
  9. //the layout resource file for the list items
  10. int resource;
  11. //constructor initializing the values
  12. public ListAdapter(Context context, int resource, List&lt;PlantTags&gt; tags, boolean childFragment) {
  13. super(context, resource, tags);
  14. this.context = context;
  15. this.resource = resource;
  16. this.childFragment = childFragment;
  17. this.tags = tags;
  18. }
  19. //constructor initializing the values
  20. public ListAdapter(Context context, int resource, List&lt;Cats&gt; cats, boolean childFragment, int categoryId) {
  21. super(context, resource, cats);
  22. this.context = context;
  23. this.resource = resource;
  24. this.childFragment = childFragment;
  25. this.categories = cats;
  26. this.categoryId = categoryId;
  27. }
  28. //this will return the ListView Item as a View
  29. @NonNull
  30. @Override
  31. public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  32. //we need to get the view of the xml for our list item
  33. //And for this we need a layoutinflater
  34. LayoutInflater layoutInflater = LayoutInflater.from(context);
  35. //getting the view
  36. View view = layoutInflater.inflate(resource, null, false);
  37. //getting the view elements of the list from the view
  38. ImageView imageView = view.findViewById(R.id.imageView);
  39. TextView textViewName = view.findViewById(R.id.category_name);
  40. if (!childFragment) {
  41. //getting the hero of the specified position
  42. Cats hero = categories.get(position);
  43. //adding values to the list item
  44. textViewName.setText(hero.getTitle());
  45. } else {
  46. //getting the hero of the specified position
  47. PlantTags tag = tags.get(position);
  48. //adding values to the list item
  49. if (tag.getCategoryId() == categoryId) {
  50. textViewName.setText(tag.getTitle());
  51. }
  52. }
  53. return view;
  54. }
  55. }

** Pojo model for PlantTags **

  1. public class PlantTags {
  2. @Expose
  3. @SerializedName(&quot;Title&quot;)
  4. private String Title;
  5. @Expose
  6. @SerializedName(&quot;CategoryId&quot;)
  7. private int CategoryId;
  8. @Expose
  9. @SerializedName(&quot;Id&quot;)
  10. private int Id;
  11. @Expose
  12. @SerializedName(&quot;$id&quot;)
  13. private String $id;
  14. public String getTitle() {
  15. return Title;
  16. }
  17. public void setTitle(String Title) {
  18. this.Title = Title;
  19. }
  20. public int getCategoryId() {
  21. return CategoryId;
  22. }
  23. public void setCategoryId(int CategoryId) {
  24. this.CategoryId = CategoryId;
  25. }
  26. public int getId() {
  27. return Id;
  28. }
  29. public void setId(int Id) {
  30. this.Id = Id;
  31. }
  32. public String get$id() {
  33. return $id;
  34. }
  35. public void set$id(String $id) {
  36. this.$id = $id;
  37. }
  38. }

error
自定义数组超级方法不接受该列表。

can anyone explain where the problem is?

答案1

得分: 1

你的超类构造函数必须接受四个或五个参数之一:

  1. public ListAdapter(Context context, int resource, List<PlantTags> tags, boolean childFragment)

或者

  1. public ListAdapter(Context context, int resource, List<Cats> cats, boolean childFragment, int categoryId)

你需要填充所有四到五个参数,或者创建一个带有较少参数的新构造函数。

  1. super(context, resource, tags)

永远不会起作用,因为你试图调用的构造函数(带有三个参数的那个)不存在。

英文:

Your superclass constructor has to take either four or five parameters being:

  1. public ListAdapter(Context context, int resource, List&lt;PlantTags&gt; tags, boolean childFragment)

or

  1. public ListAdapter(Context context, int resource, List&lt;Cats&gt; cats, boolean childFragment, int categoryId)

You need to fill all four-five parameters or create a new constructor with less.

  1. 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.

huangapple
  • 本文由 发表于 2020年9月16日 07:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63911072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定