Search code examples
javaandroidlistviewcustom-adapter

Using Multiple Views in a Custom Listview Adapter


I am currently in the middle of a project, which involves one Imageview, and one Textview, set up within a custom layout file, and maintained through a custom adapter I have set up.

Everything works fine as it is, the thing is, I would like to have two or even four textviews, and passing in another string array gives me an error. I am a bit stuck here, and will place my code below for you.

The code:

The error:

I am sure it is something rather simple I am doing wrong, but either way it is beyond me.

The error can be seen in the image above. How can I do what I am currently doing, but with 2 TVs?


Solution

  • You need to use the model class below

    import java.io.Serializable;
    
    public class ListModel implements Serializable {
    
        private String name;
        private String address;
        private String image;
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getImage() {
            return image;
        }
    
        public void setImage(String image) {
            this.image = image;
        }
    }
    

    And in your java class, u need to inflate the data like this

    ArrayList<ListModel> arrayList = new ArrayList<>();
    
    for (int i = 0; i <10; i++) {
    
        ListModel model = new ListModel();
    
        model.setName("Name " + i);
        model.setAddress("Address " + i);
        model.setImage("Image " + i);
    
        arrayList.add(model);
    }
    
    // YOU CAN USE THIS LIST IN YOUR ADAPTER JUST PASS YOUR ARRAYLIST INTO ADAPTER
    CustomCurationAdaptor adapter = new CustomCurationAdaptor(this, arrayList);