Android Question pass json to AutocompleteEditText

apty

Active Member
Licensed User
Longtime User
i have the below json that i want to pass to autocomplete edit text

B4X:
Dim parser As JSONParser
parser.Initialize(JobString)
Dim root As Map = parser.NextObject
Dim predictions As List = root.Get("predictions")
For Each colpredictions As Map In predictions
Dim reference As String = colpredictions.Get("reference")
Dim types As List = colpredictions.Get("types")
For Each coltypes As String In types
Next
Dim matched_substrings As List = colpredictions.Get("matched_substrings")
For Each colmatched_substrings As Map In matched_substrings
  Dim offset As Int = colmatched_substrings.Get("offset")
  Dim length As Int = colmatched_substrings.Get("length")
Next
Dim terms As List = colpredictions.Get("terms")
For Each colterms As Map In terms
  Dim offset As Int = colterms.Get("offset")
  Dim value As String = colterms.Get("value")
Next
Dim structured_formatting As Map = colpredictions.Get("structured_formatting")
Dim main_text_matched_substrings As List = structured_formatting.Get("main_text_matched_substrings")
For Each colmain_text_matched_substrings As Map In main_text_matched_substrings
  Dim offset As Int = colmain_text_matched_substrings.Get("offset")
  Dim length As Int = colmain_text_matched_substrings.Get("length")
Next
Dim secondary_text As String = structured_formatting.Get("secondary_text")
Dim main_text As String = structured_formatting.Get("main_text")
Dim description As String = colpredictions.Get("description")
Dim id As String = colpredictions.Get("id")
Dim place_id As String = colpredictions.Get("place_id")
Next
Dim status As String = root.Get("status")

i added a list after "description" above so that i can keep the descriptions in a list and later add the list to autocomplete edit text in textchanged event. However, the list returned is always empty. What am i doing wrong?
 

apty

Active Member
Licensed User
Longtime User
here is the full code

B4X:
Sub JsonTree(JobString As String)
    Dim parser As JSONParser
    parser.Initialize(JobString)
    Dim root As Map = parser.NextObject
    Dim predictions As List = root.Get("predictions")
    For Each colpredictions As Map In predictions
        Dim reference As String = colpredictions.Get("reference")
        Dim types As List = colpredictions.Get("types")
        For Each coltypes As String In types
        Next
        Dim matched_substrings As List = colpredictions.Get("matched_substrings")
        For Each colmatched_substrings As Map In matched_substrings
            Dim offset As Int = colmatched_substrings.Get("offset")
            Dim length As Int = colmatched_substrings.Get("length")
        Next
        Dim terms As List = colpredictions.Get("terms")
        For Each colterms As Map In terms
            Dim offset As Int = colterms.Get("offset")
            Dim value As String = colterms.Get("value")
        Next
        Dim structured_formatting As Map = colpredictions.Get("structured_formatting")
        Dim main_text_matched_substrings As List = structured_formatting.Get("main_text_matched_substrings")
        For Each colmain_text_matched_substrings As Map In main_text_matched_substrings
            Dim offset As Int = colmain_text_matched_substrings.Get("offset")
            Dim length As Int = colmain_text_matched_substrings.Get("length")
        Next
        Dim secondary_text As String = structured_formatting.Get("secondary_text")
        Dim main_text As String = structured_formatting.Get("main_text")
        Dim description As String = colpredictions.Get("description")
        PlaceList.Add(description)
        Dim id As String = colpredictions.Get("id")
        Dim place_id As String = colpredictions.Get("place_id")
        PlaceList.Add(description)
    Next
    Dim status As String = root.Get("status")
   
End Sub
Sub AutoCompleteEditText1_TextChanged (Old As String, New As String)
    PlaceList.Initialize
    KeyWord=AutoCompleteEditText1.Text   
    AutoCompleteEditText1.SetItems(PlaceList)
End Sub
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
sorry the description is supposed to be added only once, but even that way the list is still empty. I used a json from google place autocomplete and pasted it in
https://b4x.com:51041/json/index.html then it gave me the above code.

setitems is called at textchanged because i want whenever i have new text it is searched in google places autocomplete so that i can select the place from the list
I still get an empty list
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code sets an empty list. It cannot work.
B4X:
Sub AutoCompleteEditText1_TextChanged (Old AsString, New AsString)
 PlaceList.Initialize
 KeyWord=AutoCompleteEditText1.Text 
 AutoCompleteEditText1.SetItems(PlaceList)
End Sub

I don't think that AutoCompleteEditText will work properly if you change the items in the TextChanged event. You should use a regular EditText with ListView instead.
 
Upvote 0
Top