Hey guys i need a quick help here I have a model ...
# android
k
Hey guys i need a quick help here I have a model and i want to access the items outside a for loop
Copy code
var model:User()
forEachIndexed { index, users ->{
println("my $users ")
}
println("I want to access my result outside for loop")
e
What kind of result do you want?
k
i want to get back the model result which match with like
println("Result $model")
... Not an arraylist
e
Then what object does you actually looping?
k
Am looping into this arraylist
val arraylist = ArrayList<User>()
Example
Copy code
val arraylist = ArrayList<User>()
var model:User()
forEachIndexed { index, users ->{
println("my $users ")
}
println("Result $model")
e
I think you should be calling
arraylist.forEachIndexed
?
k
but i want the model
e
And if you are looping the
ArrayList<User>
, then you can use
filter{}
to get the matches, or even
first {}
if you just need one result
k
i actually calling
arraylist.forEachIndexed
Copy code
val arraylist = ArrayList<User>()
var model:User()
arraylist.forEachIndexed { index, users ->{
println("my $users ")
}
println("Result $model")
where i need help is to access that model ... which is not an arraylist model just a model like above
e
If I didn't get it wrong, you want to set
var model
to the first one that matches some condition inside the
val arraylist
?
k
i want to access all the result after looping the array as
var model
which i declare up ...
to give u more clarity i have a `fun which i need to pass that
model
it cannot be an
array
e
Can't you just call that
fun
inside the loop?
k
no it will keep looping all the values
e
If you don't want to call that
fun
for every
User
in the list, then use
filter
to get rid of the unwanted
User
first, then
map
each
User
to the result of that
fun
k
i will just pass my
fun
inside a
for
cz filtering is not helping
Am i doing it right like this
Copy code
var getuser:List<User> = arralist.filter { user -> User ==User() }

            println("FILTER 909 $getuser")
            getuser.map { dataModel ->
                log("DADADADADDAAD $dataModel")
            }
e
Copy code
fun action(user: User): T = ...
val arraylist: ArrayList<User>
arraylist.filter { user -> user == ... }.map { action(it) }
If
action()
does not return a value, replace the
.map
with
.forEach
k
when filtering can i replace all the model ? like
arraylist.filter { user -> user == User() }.map
e
Oh then you should do
.map { User() }
k
``` arraylist.filter { user -> user == User()}.map {User() println("DDDD ----------- $it") }``that line ain't executed
e
Cause nothing matches
user == User()
filter
is not meant for manipulating data
k
how tp get something which matches ?
e
Don't understand what you want to ask
To clarify the problem, you have a
ArrayList<User>
, and you want to pass some of them into a function
fun action(user: User): Result
, and gather all the results. Am I getting it wrong, or you want to actually manipulate the
ArrayList<User>
?
k
u are getting it right
it like that .. from my
ArrayList<User>
i want to access the model by looping
e
Then its simple as just
users.filter { boolean condition }.map { user -> getResult(user) }
This will gives you back
List<Result>
which you can then operate on