https://kotlinlang.org logo
#android
Title
# android
k

Kevin Janvier Chinabalire

01/31/2018, 10:08 AM
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

edwardwongtl

01/31/2018, 10:20 AM
What kind of result do you want?
k

Kevin Janvier Chinabalire

01/31/2018, 10:22 AM
i want to get back the model result which match with like
println("Result $model")
... Not an arraylist
e

edwardwongtl

01/31/2018, 10:24 AM
Then what object does you actually looping?
k

Kevin Janvier Chinabalire

01/31/2018, 10:24 AM
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

edwardwongtl

01/31/2018, 10:27 AM
I think you should be calling
arraylist.forEachIndexed
?
k

Kevin Janvier Chinabalire

01/31/2018, 10:28 AM
but i want the model
e

edwardwongtl

01/31/2018, 10:28 AM
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

Kevin Janvier Chinabalire

01/31/2018, 10:28 AM
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

edwardwongtl

01/31/2018, 10:31 AM
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

Kevin Janvier Chinabalire

01/31/2018, 10:33 AM
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

edwardwongtl

01/31/2018, 10:35 AM
Can't you just call that
fun
inside the loop?
k

Kevin Janvier Chinabalire

01/31/2018, 10:37 AM
no it will keep looping all the values
e

edwardwongtl

01/31/2018, 10:39 AM
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

Kevin Janvier Chinabalire

01/31/2018, 10:56 AM
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

edwardwongtl

01/31/2018, 11:00 AM
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

Kevin Janvier Chinabalire

01/31/2018, 11:05 AM
when filtering can i replace all the model ? like
arraylist.filter { user -> user == User() }.map
e

edwardwongtl

01/31/2018, 11:06 AM
Oh then you should do
.map { User() }
k

Kevin Janvier Chinabalire

01/31/2018, 11:09 AM
``` arraylist.filter { user -> user == User()}.map {User() println("DDDD ----------- $it") }``that line ain't executed
e

edwardwongtl

01/31/2018, 11:10 AM
Cause nothing matches
user == User()
filter
is not meant for manipulating data
k

Kevin Janvier Chinabalire

01/31/2018, 11:15 AM
how tp get something which matches ?
e

edwardwongtl

01/31/2018, 11:19 AM
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

Kevin Janvier Chinabalire

01/31/2018, 12:04 PM
u are getting it right
it like that .. from my
ArrayList<User>
i want to access the model by looping
e

edwardwongtl

01/31/2018, 12:13 PM
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
4 Views