For a = myList.Size -1 to 0 Step -1
Log(id(1))
Log(id(2))
Log(id(3))
Log(id(4))
Log(id(5)) 'It will error here and beyond because there is no 5......
Next
lstID As List
lstID.Add(5)
lstID.Add(53)
lstID.Add(11)
lstID.Add(2343)
lstID.Add(22)
For Each ID As Int In lstID
Log(ID)
Next
For I = 0 to lstID.Size - 1
Log(lstID.Get(I))
Next
For I = 0 to IDs.Lenght - 1
Log(IDs(I))
Next
Dim parser As JSONParser
parser.Initialize(str)
Dim root As Map = parser.NextObject
Dim myList As List = root.Get("myList")
Dim resSize As Int = myList.size
Dim id(resSize),vote_count(resSize) As Int
Dim vote_average(resSize) As Double
Dim title(resSize), release_date(resSize), original_title(resSize),video(resSize) As String
Dim c As Int = 0
For Each colresults As Map In myList
id(c) = colresults.Get("id")
title(c) = colresults.Get("title")
vote_average(c) = colresults.Get("vote_average")
release_date(c) = colresults.Get("release_date")
original_title(c) = colresults.Get("original_title")
vote_count(c) = colresults.Get("vote_count")
video(c) = colresults.Get("video")
c = c + 1
Next
For a = 0 To myList.Size -1
Log("record: " & a)
Log (id(a))
Log (title(a))
Log (vote_average(a))
Log (release_date(a))
Log (original_title(a))
Log (vote_count(a))
Log (video(a))
Log("-----------------------" & CRLF)
Next
LucasMs. ( Thanks Your response could not have been any faster)
This is the code I am working with:
Log(id(a) will retrieve ALL the IDS.B4X:Dim parser As JSONParser parser.Initialize(str) Dim root As Map = parser.NextObject Dim myList As List = root.Get("myList") Dim resSize As Int = myList.size Dim id(resSize),vote_count(resSize) As Int Dim vote_average(resSize) As Double Dim title(resSize), release_date(resSize), original_title(resSize),video(resSize) As String Dim c As Int = 0 For Each colresults As Map In myList id(c) = colresults.Get("id") title(c) = colresults.Get("title") vote_average(c) = colresults.Get("vote_average") release_date(c) = colresults.Get("release_date") original_title(c) = colresults.Get("original_title") vote_count(c) = colresults.Get("vote_count") video(c) = colresults.Get("video") c = c + 1 Next For a = 0 To myList.Size -1 Log("record: " & a) Log (id(a)) Log (title(a)) Log (vote_average(a)) Log (release_date(a)) Log (original_title(a)) Log (vote_count(a)) Log (video(a)) Log("-----------------------" & CRLF) Next
But let's say I want ONLY id 4 not knowing if id 4 exists will throw error outofbounds. That's my problem.
How do I retrieve each id(1), id(2) etc... individually without knowing what the last Id is and running into the outofbound error?
you know the max index you can use, resSize-1 (or myList.Size -1, it is the same).Dim resSize As Int = myList.size
Dim id(resSize),vote_count(resSize) As Int
If MyList.Size >= 4 Then
FoundID = MyList.Get(3)
' or, as in your example:
FoundID = ID(3)
End IF
For a = 0 To myList.Size -1
Log("record: " & a)
If id.size > a then Log (id(a))
If title.size > a then Log (title(a))
If vote_average.size > a then Log (vote_average(a))
If release_date.size > a then Log (release_date(a))
If original_title.size > a then Log (original_title(a))
If vote_count.size > a then Log (vote_count(a))
If video.size > a then Log (video(a))
Log("-----------------------" & CRLF)
Next
what you need to do if you have to do it with a single loop is this :
B4X:For a = 0 To myList.Size -1 Log("record: " & a) If id.size > a then Log (id(a)) If title.size > a then Log (title(a)) If vote_average.size > a then Log (vote_average(a)) If release_date.size > a then Log (release_date(a)) If original_title.size > a then Log (original_title(a)) If vote_count.size > a then Log (vote_count(a)) If video.size > a then Log (video(a)) Log("-----------------------" & CRLF) Next
@LucaMs @jmonYou could use try-catch but... if you have a List, you know how many items it has!
If you need the fourth item (index 3):
B4X:If MyList.Size >= 4 Then FoundID = MyList.Get(3) ' or, as in your example: FoundID = ID(3) End IF
Last try:
MyList.IndexOf is a function of List; it returns -1 if the item is not found
Try
If id(0) >= a Then Log(id(0))
If id(1) >= a Then Log(id(1))
If id(2) >= a Then Log(id(2))
If id(3) >= a Then Log(id(3))
If id(4) >= a Then Log(id(4))
If id(5) >= a Then Log(id(5))
If id(6) >= a Then Log(id(6))
If id(7) >= a Then Log(id(7))
If id(8) >= a Then Log(id(8))
If id(9) >= a Then Log(id(9))
If id(10) >= a Then Log(id(10))
If id(11) >= a Then Log(id(11))
If id(12) >= a Then Log(id(12))
If id(13) >= a Then Log(id(13))
If id(14) >= a Then Log(id(14))
If id(15) >= a Then Log(id(15))
If id(16) >= a Then Log(id(16))
If id(17) >= a Then Log(id(17))
If id(18) >= a Then Log(id(18))
If id(19) >= a Then Log(id(19))
If id(20) >= a Then Log(id(20))
If id(21) >= a Then Log(id(21))
If id(22) >= a Then Log(id(22))
If id(23) >= a Then Log(id(23))
If id(24) >= a Then Log(id(24))
If id(25) >= a Then Log(id(25))
Catch
Log("id is empty")
End Try
Dim parser As JSONParser
parser.Initialize(str)
Dim root As Map = parser.NextObject
Dim myList As List = root.Get("myList")
Dim resSize As Int = myList.size
Dim id(resSize),vote_count(resSize) As Int
Dim vote_average(resSize) As Double
Dim title(resSize), release_date(resSize), original_title(resSize),video(resSize) As String
Dim c As Int = 0
For Each colresults As Map In myList
id(c) = colresults.Get("id")
title(c) = colresults.Get("title")
vote_average(c) = colresults.Get("vote_average")
release_date(c) = colresults.Get("release_date")
original_title(c) = colresults.Get("original_title")
vote_count(c) = colresults.Get("vote_count")
video(c) = colresults.Get("video")
c = c + 1
Next
Sorry, I don't understand.
The code above (your code) works?
If so, you know that you will have id(0) to id(resSize - 1)
Or you can check id.Lenght
If id.Lenght will be = 10, I'm sure that you will not have id(10), id(11), ...!
How they "select" the id?But the user(s) don't know how many ids there are in the list and if they select id(11)
Good advice.(I would avoid things like: id(gotonum.Text). Dim GoToNum As Int and then GoToNum = gotonum.text is better).
I suppose that gotonum.Text is where the user "selects" the index of id and IDText.Text shows the choice (a label would be better).
You need to validate gotonum.Text value, inside of gotonum_TextChanged or gotonum_FocusChanged or elsewhere, before set IDText.Text = id(GoToNum).
[P.S. validate: I mean: If GoToNum > id.Length-1 then ... not valid]