Amir Dora.
Amir Dora.

Reputation: 2707

Custom array super method not accepting the list

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;
    }
}

error enter image description here

can anyone explain where the problem is?

Upvotes: 1

Views: 42

Answers (2)

Amir Dora.
Amir Dora.

Reputation: 2707

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.

Upvotes: 0

NotZack
NotZack

Reputation: 518

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.

Upvotes: 1

Related Questions