有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将两个不同类型的列表组合在一起

我正在尝试合并两个不同类型的列表。 我在安卓应用程序中得到了两个不同的API响应,第一个列表是定义为

公共类列表{

@SerializedName("times")
@Expose
private List<String> times = null;
@SerializedName("title")
@Expose
private String title;

/**
 * No args constructor for use in serialization
 *
 */
public Listing() {
}

/**
 *
 * @param title movie title
 * @param times movie times
 */
public Listing(List<String> times, String title) {
    super();
    this.times = times;
    this.title = title;
}

public List<String> getTimes() {
    return times;
}

public void setTimes(List<String> times) {
    this.times = times;
}

public Listing withTimes(List<String> times) {
    this.times = times;
    return this;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Listing withTitle(String title) {
    this.title = title;
    return this;
}

}

我使用以下命令成功地从API检索列表

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    ListingApiService listingApiService = retrofit.create(ListingApiService.class);

    Call<ListingResponse> call = listingApiService.getShowtimes(id);
    call.enqueue(new Callback<ListingResponse>() {
        @Override
        public void onResponse(Call<ListingResponse> call, Response<ListingResponse> response)
        {
            List<Listing> listings = response.body().getListings();

然后,我尝试将类型列表列表与类型字符串列表相结合,以创建一个新类ListingAndImage(该字符串是我将加载到imageview中的URL)

public class ListingAndImage
{
    Listing listing;
    String image;

    public ListingAndImage(Listing listing, String image) {
        this.listing = listing;
        this.image = image;
    }
}

我的问题是,哪种方式最好将这两个列表合并成一个列表和图像,然后由我的recyclerview向用户显示

或者,有没有更好的方法,比如将两个列表都传递给我的recyclerview

多谢各位


共 (1) 个答案

  1. # 1 楼答案

    您好,您正在寻找的是具有多种视图类型的recyclerView,通过简单的谷歌搜索,您将获得大量代码示例。 您可以搜索诸如异构recyclerView或具有多种视图类型的recyclerView之类的术语,并遵循这些示例。 如果你想使用这个库,你可以试试“环氧树脂”