API Reference / Android Widgets / Filter Numeric Comparison

Filter Numeric Comparison

About this widget

Filter Numeric Comparison is a view to filter on a numeric value using a comparison operator.

To add a filter numeric comparison to your search experience, you can use these components:

  • Searcher: the Searcher that handles your searches.
  • FilterState: the current state of the filters.
  • NumberViewModel<T>: the logic applied to the numeric value.
  • NumberView<T>: the view that renders the numeric value.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class FilterPriceView(
    val textView: TextView,
    val arrowUp: ImageView,
    val arrowDown: ImageView,
    val price: Attribute,
    val operator: NumericOperator
): NumberView<Int> {

    init {
        textView.text = "$price ${operator.raw}"
    }

    override fun setComputation(computation: Computation<Int>) {
        arrowUp.setOnClickListener {
            computation.increment()
        }
        arrowDown.setOnClickListener {
            computation.decrement()
        }
    }

    override fun setText(text: String) {
        textView.text = text
    }
}

class MyActivity : AppCompatActivity() {

    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    val filterState = FilterState()
    val price = Attribute("price")
    val operator = NumericOperator.Greater
    val viewModel = NumberViewModel(range = 0..10)
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val textView = TextView(this)
        val arrowUp = ImageView(this)
        val arrowDown = ImageView(this)
        val view: NumberView<Int> = FilterPriceView(textView, arrowUp, arrowDown, price, operator)

        searcher.query.addFacet(price)

        connection += searcher.connectFilterState(filterState)
        connection += viewModel.connectView(view)
        connection += viewModel.connectFilterState(filterState, price, operator)

        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connection.disconnect()
        searcher.cancel()
    }
}

Parameters

searcher
type: Searcher
Required

The Searcher that handles searches.

1
searcher.connectFilterState(filterState)
filterState
type: FilterState
Required

The FilterState that holds filters.

1
viewModel.connectFilterState(filterState)
view
type: NumberView<T>
Required

The view that renders the numeric value.

1
viewModel.connectView(view)
attribute
type: Attribute
Required

The attribute to filter on.

1
2
3
val attribute = Attribute("price")

viewModel.connectFilterState(attribute = attribute, filterState)
range
type: ClosedRange<T>
Optional

The limits of the acceptable range within which values are coerced.

1
NumberViewModel(0..10)
operator
type: NumericOperator
Required

The NumericOperator used to perform a numeric comparison.

1
viewModel.connectFilterState(filterState, price, operator = NumericOperator.Greater)
groupID
type: FilterGroupID
default: FilterGroupID(attribute.raw)
Optional

Groups all created filters under an ID and composes them with this operator. Defaults to the used attribute, with FilterOperator.And between filters in this group.

1
2
3
4
val price = Attribute("price")
val groupID = FilterGroupID(price, FilterOperator.And)

viewModel.connectFilterState(price, filterState, groupID = groupID)

Did you find this page helpful?

InstantSearch Android v2