Cyrus
Cyrus

Reputation: 9425

How to filter a list and collect it's index to a string result use RxJava2?

This is my code to collect checkbox index that checked and get a string result.

  StringBuilder sb = new StringBuilder();
    for (int i = 0; i < checkBoxList.size(); i++) {
        if (checkBoxList.get(i).isChecked()) {
            sb.append((i + 1) % 7 + 1);
            sb.append(",");
        }
    }

But i want to implement it use RxJava2 like this.

Observable.fromArray(checkBoxList).filter(..).subscribe(..)....

Who has ideas?
Thanks for first!

Upvotes: 2

Views: 585

Answers (1)

akarnokd
akarnokd

Reputation: 69997

I suggest reading a book or introductory blog post about RxJava where you will find the basic building blocks for this type of flow:

  Observable.range(0, checkBoxList.size())
  .filter(idx -> checkBoxList.get(idx).isChecked())
  .collect(StringBuilder::new, (sb, idx) -> 
       sb.append((idx + 1) % 7 + 1).append(",")
  );

Upvotes: 4

Related Questions