RxJava2:过滤Observable中的List <object>

时间:2017-09-06 16:28:27

标签: android rx-java rx-java2

我是Rx编程的新手,我正在尝试在项目中使用它。我想要的是我有一个GamesInfoList的对象,如下所示:

@AutoValue
public abstract class GameInfoList {
    public static TypeAdapter<GameInfoList> typeAdapter(Gson gson) {
        return new AutoValue_GameInfoList.GsonTypeAdapter(gson);
    }

    public static Builder builder() {
        return new AutoValue_GameInfoList.Builder();
    }

    public abstract long id();

    @Nullable
    public abstract String date_added();

    @Nullable
    public abstract String date_last_updated();

    @Nullable
    public abstract GameImages image();

    @Nullable
    public abstract String name();

    @Nullable
    public abstract List<GamePlatformInfo> platforms();

    @AutoValue.Builder
    public abstract static class Builder {

        public abstract Builder id(long id);

        public abstract Builder date_added(String date_added);

        public abstract Builder date_last_updated(String date_last_updated);

        public abstract Builder image(GameImages image);

        public abstract Builder name(String name);

        public abstract Builder platforms(List<GamePlatformInfo> platforms);

        public abstract GameInfoList build();
    }
}

现在,我想按日期和名称加载我的游戏,然后我写了一个方法来访问其中的上述对象。

public void loadGamesByDateAndName(String date, String name) {
    Timber.d("Load games by date: " + date + " and name: " + name);
    remoteDataHelper
            .getGamesListByDate(date)
            .flatMapObservable(Observable::fromIterable)
            .filter(game -> game.platforms() != null)
            .filter(game -> game.platforms().name().contains(name))
            .toList()
            .subscribe(getObserverFiltered(name, false));
}

这里,我根据此方法中输入的名称过滤(使用filter()运算符)平台对象。上面的方法显示错误,因为平台是List类型。如何在列表中进行过滤以匹配我的Android应用中的名称和显示?为了按日期和名称加载数据,需要对上述方法进行哪些更改?

2 个答案:

答案 0 :(得分:3)

最简单的方法是在第二个filter运算符内迭代。

public void loadGamesByDateAndName(String date, String name) {
    Timber.d("Load games by date: " + date + " and name: " + name);
    remoteDataHelper
            .getGamesListByDate(date)
            .flatMapObservable(Observable::fromIterable)
            .filter(game -> game.platforms() != null)
            .filter(game -> { for(GamePlatformInfo platform : game.platforms()){
                                  if(platform.name().contains(name)) {
                                      return true;
                                  }
                              }
                              return false;
                    }
            )
            .toList()
            .subscribe(getObserverFiltered(name, false));
}

答案 1 :(得分:-1)

您需要覆盖crx-quickstart/conf/controlport类中的equals()方法。

我假设你有以下GamePlatformInfo类的类定义:

GamePlatformInfo

然后你只需要使用class GamePlatformInfo{ String name; ... ... public GamePlatformInfo(String name){ this.name = name; ... } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof GamePlatformInfo)) return false; final GamePlatformInfo other = (GamePlatformInfo)o; return name.equals(other.name); } @Override public int hashCode() { int result = 17; result = 31 * result + name.hashCode(); return result; } } 运算符来搜索该列表。 检查以下修改后的代码段:

contains()

引擎盖:如果您创建一个覆盖public void loadGamesByDateAndName(String date, String name) { Timber.d("Load games by date: " + date + " and name: " + name); remoteDataHelper .getGamesListByDate(date) .flatMapObservable(Observable::fromIterable) .filter(game -> game.platforms() != null) .filter(game -> game.platforms().contains(new GamePlatformInfo(name))) // This is all you need to do to search through the list. .toList() .subscribe(getObserverFiltered(name, false)); } 方法的类列表,则equals()方法使用给定的equals定义来比较列表项。

我希望它有所帮助。