Using Multiple Views in a Custom Listview Adapter

妖精的绣舞 提交于 2019-12-13 03:34:31

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/51960482/using-multiple-views-in-a-custom-listview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!