How can i change a Field in a List of Objects with RXJava/Android

5.2k views Asked by At

I have a List myPersonList an i wanna fill the Name and the City of every item. I have the id's already filled. I could create a foreach and call the FillPersonData but how can i do this with Observables? I know there is something with Map() and FlatMap() ... but i don't get it

class Person{
   int id;
   String name;
   String city;
}

List<Person> myPersonList;

what i have to do to get an observable List where the Person Data are filled by this Function:

class PersonUtil{

      public Person FillPersonData(Person person){
          ... do something to fill the Person Data...
      }
}

Update:

Sorry my Fault: i didn't say that i also need the Observable back to subscribe on it AND not the Person Class has a Function named fillPersonData()... let me say i have something like PersonUtil...

So i need this to return it:

Observable<List<Person>> myObservable  = ...the Magic Code :-)

Update 2:

I tried the Answer from frhack and it works. Just for completeness because i work in Android Studio without Lambdas here the Code without Lambdas:

 Observable<Person> observable =  Observable.from(personList)
            .filter(new Func1<Person, Boolean>() {

                @Override
                public Boolean call(Person person) {
                    PersonUtil.FillMailData(person);
                    return true;
                }

            });

I hope is the right Way... if NOT... tell me that and i change my Update2!!

1

There are 1 answers

5
frhack On BEST ANSWER
List<Person> personList = new ArrayList<Person>();
// populate personList
Observable.from(personList).forEach((person)-> {
        person.setName(name);
        person.setCity(city);
});

alternate version:

List<Person> personList = new ArrayList<Person>();
// populate personList
Observable.from(personList).forEach((person)-> p.fillPersonData() );

Alternate version:

List<Person> personList = new ArrayList<Person>();
// populate personList
Observable<Person> observable =  Observable.from(personList)
   .filter((person) -> {
            person.setName(name);
           //PersonUtil.fillPersonData(person)     
            return true;
        });
 observable.forEach((pp)->System.out.println(pp));