Search code examples
javaandroidlistviewlistview-adapter

How to tell Custom adapter to use my list from class instead of filling\adding items?


I try to show my listView with text and images, as well made a customAdapter like in a video I had seen (I am very new person in android programming) And when in my MainActivity.class I try to use the adapter it recuires me to fill it with a list, like in that exmple.

CustomListAdapter listAdapter = new CustomListAdapter(this,imageItems);

And before fill it like this

 private void fillList() {
        imageItems = new ArrayList<>();
        imageItems.add(new ImageItem(R.drawable.aloner,"Title 1"));
        imageItems.add(new ImageItem(R.drawable.years,"Title 2"));
        imageItems.add(new ImageItem(R.drawable.zombie,"Title 3"));
        imageItems.add(new ImageItem(R.drawable.fighter,"Title 4"));
        imageItems.add(new ImageItem(R.drawable.alonefighter,"Title 5"));
    }

But I already have in my ImageList.class a List that I need.

 public static final ImageItem[] images = {
            new ImageItem(R.drawable.aloner, "Title 1"),
            new ImageItem(R.drawable.years, "Title 2"),
            new ImageItem(R.drawable.zombie, "Title 3"),
            new ImageItem(R.drawable.fighter, "Title 4"),
            new ImageItem(R.drawable.alonefighter, "Title 5")
    };

I tried to fill the adapter with my ImageList.images but it shows me an error and demands a list. enter image description here

How can I tell my CustomAdapter to use my created list from ImageItem.class instead of filling it in my MainActivity?

Hopefully I have explained my issue right.In all the videos I have watched the people always were filling their lists...


Solution

  • The error says clearly that there is a difference in type of the arguments in the constructor for the custom adapter and the one which you are trying to set.

    Either you need to change the constructor of the Custom adapter to use an array instead of a list or while making a call to the constructor, you may need to convert the array to a list like

    CustomListAdapter listAdapter = new CustomListAdapter(this, Arrays.asList(ImageItem.images));