简体   繁体   中英

Combine two lists of different types

I am trying to combine two lists of different types. I am getting two different API responses from retrofit inside an android application, the first list is a list of cinema listings defined as

public class Listing {

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

}

I successfully retrieve the list from the API with the following

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

I am then attempting to combine a list of type listings with a list of type string to create a new class, ListingAndImage (the string is a URL which I will load into an imageview)

public class ListingAndImage
{
    Listing listing;
    String image;

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

My question is, which way would be best to combine the two lists to make a list of listing and images, which would then be used by my recyclerview to display to the user?

Or, alternatively, is there a better way of doing this such as passing both lists to my recyclerview?

Thank you

Hi what you are looking for is recyclerView with multiple view types you will get lots of code samples with simple google search. you can search for terms like heterogeneous recyclerView or recyclerView with multiple view types and follow those samples. If you want to use library for this you can try "Epoxy".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM