MARSH
MARSH

Reputation: 271

how to filter data in kotlin based on item

this is method where i am getting data month list but when i try to filter with two filed i am getting compile error

  override fun onGraphDataLoaded(data: List<GraphData>?) {
    Log.d("DATAITEM", data?.size.toString())
    var childList: List<GraphData>? = data?.let { it.filter { s -> s.isChild && t -> t.beginningTime>0 } }
    var parentList: List<GraphData>? = data?.let { it.filter { s -> !s.isChild } }
    Log.d("DATAITEM", "ChildSize"+childList?.size)
    Log.d("DATAITEM", "ParentSize"+parentList?.size)

}

this is my model class

data class GraphData(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    var beginningTime: Long? = 0L,
    var endTime: Long? = 0L,
    var isChild: Boolean
)

i want to filter data based on ischild and beginningTime>0 but it show compile time error please help me in this .

Upvotes: 1

Views: 785

Answers (2)

devik
devik

Reputation: 21

You should check the "isChild Boolean variable" with "true" or "false"

override fun onGraphDataLoaded(data: List<GraphData>?) {
   Log.d("DATAITEM", data?.size.toString())
   var monthList: List<GraphData>? = data?.let { it.filter {s->s.isChild ==ntrue
   } 
} 

Upvotes: 0

k314159
k314159

Reputation: 11120

You're using both s and t as lambda parameters. You can't change the name of the lambda parameter in the middle of an expression!

Also, you're comparing a nullable property to an int. You need to take care of its nullability.

Thirdly, there's no need for .let - that makes the line more complicatted. Just filter directly.

var childList = data?.filter { it.isChild && (it.beginningTime ?: 0) > 0 }

Upvotes: 2

Related Questions