stanna stanna - 1 month ago 17
Android Question

How would you iterate through a list in retrolambda?

I have the following code

List<Trailer> trailers = response.body().getResults();
trailers.stream().forEach(trailer -> {
View reviewView = LayoutInflater.from(mContext).inflate(R.layout.trailer_list_item, null);
((TextView) reviewView.findViewById(R.id.movies_trailers_list)).setText(trailer.getName());
LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.movie_trailers);
linearLayout.addView(reviewView);
});


is there a way to get this same code to work with retrolambda.
I need to iterate throught list results i get back and add it a view.

Any advice would be greatly appreciated

Answer

I'd kindly suggest to use streamsupport as an additional library for the Java 8 streams part:

        import java8.util.stream.Streamsupport;

        StreamSupport.stream(trailers).forEach(trailer -> {
            View reviewView = LayoutInflater.from(mContext).inflate(R.layout.trailer_list_item, null);
            ((TextView) reviewView.findViewById(R.id.movies_trailers_list)).setText(trailer.getName());
            LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.movie_trailers);
            linearLayout.addView(reviewView);
        });

The required changes for Java 8 stream code are usually quite small and (in most cases) mechanic.

Comments