如何使用RXjava运算符将过滤器对象列表设置为Object

时间:2018-10-28 03:44:24

标签: android rx-java2 rx-android

我需要过滤内容为空的feed列表,我尝试使用filter操作符,然后我无法返回“ FeedsResponse”,不确定如何过滤列表并返回FeedsResponse。

// Tried with Filter Operator 
 public Single<ApiResponse> getFeeds() {
    final FeedsResponse response = new FeedsResponse();
    return feedServiceService.getFeedList()
            .flatMap(new Function<FeedsResponse,
                    ObservableSource<List<Feed>>>() {
                @Override
                public ObservableSource<List<Feed>> apply(
                        FeedsResponse feedsResponse) throws Exception {
                    response.setTitle(feedsResponse.getTitle());
                    return Observable.just(feedsResponse.getFeed());
                }
            })
            .flatMap(Observable::fromIterable)
            .filter(new Predicate<Feed>() {
                @Override
                public boolean test(Feed feed) throws Exception {
                    return !(feed != null && (
                            TextUtils.isEmpty(feed.getTitle())
                            && TextUtils.isEmpty(feed.getDescription())
                            && TextUtils.isEmpty(feed.getImageHref())));
                }
            })
            .toList().map(new Function<List<Feed>, ApiResponse>() {
                @Override
                public ApiResponse apply(List<Feed> feeds) throws Exception {
                    response.setFeed(feeds);
                    return new ApiResponse(Status.SUCCESS,response,null);
                }
            });
}

--------- ?????

a)如何更好地返回Feed响应,我必须在外部创建最终对象并在最终地图中设置值,有没有更好的方法

b)列表是否为空

---------???????


//////////////////////


public class FeedRepository {
    private final FeedService feedServiceService;

    public Observable<FeedsResponse> getFeeds() {
        return feedServiceService.getFeedList();
    }
}


//-------------
//Rest API 

Observable<FeedsResponse> getFeedList();

//-------------          
//POJO    

FeedsResponse {
    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("rows")
    @Expose
    private List<Feed> feed = null;
}


public class Feed {
    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("description")
    @Expose
    private String description;

    @SerializedName("imageHref")
    @Expose
    private String imageHref;
}

//-------------

0 个答案:

没有答案