Reputation: 584
I am very new to Rx programming and I was trying my hands on it in a project. What I want is I am having an object of GamesInfoList as shown below:
@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();
}
}
Now, I want to load my games by date and name and I wrote a method for that to access this above object in it.
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));
}
Here, I am filtering (using filter() operator) the platforms object based on the name inputted in this method. The above method is showing me an error because platform is of type List. How can I filter through the list to match the name and display in my Android app? What changes need to be made to the above method in order to load data by date and name?
Upvotes: 3
Views: 2575
Reputation: 17809
Well You need to override equals()
method in GamePlatformInfo
class.
I assume you have following class definition of GamePlatformInfo
class:
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;
}
}
Then you just need to to use contains()
operator to search through that list.
Check following modified snippet:
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));
}
Under the hood: If you create a list of a class that overrides equals()
method then list.contains()
method uses given equals definition to compare items of list.
I hope it helps.
Upvotes: -1
Reputation: 3980
The simplest way is to iterate inside the second filter
operator.
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));
}
Upvotes: 3