JSON into Scrollview error

FMFREAK

Member
Licensed User
Longtime User
I have made this code, to put a JSON file into a ScrollView

B4X:
   Dim JSON As JSONParser
    Dim m, Map1 As Map
   Dim Title As String
   Dim MenuItems As List
   Dim lblTitle As Label
   Dim lblItem As Label
   iTel = 0
   lblTitle.Initialize("")
   lblItem.Initialize("")
   ScrollView1.Panel.Height=2dip * ScrollView1.Height
   ScrollView1.Panel.Color = Colors.Transparent

   Map1.Initialize()
    JSON.Initialize(File.ReadString(File.DirAssets, "nieuwsapp.json"))
    Map1 = JSON.NextObject                      
    MenuItems = Map1.Get("newsitems")            
   'MenuItems = m.Get("newsitem")
           For i = 0 To MenuItems.Size - 1                       
      m = MenuItems.Get(i)               
      m = m.Get("newsitem")               
      Title = m.Get("title")               
      Content = m.Get("content")            
      lblTitle.Text = Title
      lblItem.Text = Content
      lblTitle.TextColor = Colors.Black
      lblTitle.TextSize = 12
      lblItem.TextColor = Colors.Black
      lblItem.TextSize = 10
      ToastMessageShow(iTel & " " & Title,True)
      ScrollView1.Panel.AddView(lblTitle, 5dip, 5dip + iTel * 120dip, ScrollView1.Width, 150dip)
      'ScrollView1.Panel.AddView(lblItem, 5dip, 40dip + iTel * 120dip, ScrollView1.Width, 150dip)
      iTel=iTel+1
      'Log(m.Get("title"))
    Next

But when I run it I got the next error:

The Specified child already has a parent. You must call removeView on the child's parent first.

What did I do wrong ?
 

klaus

Expert
Licensed User
Longtime User
You need to Dim the two Labels in the For/Next loop.
B4X:
For i = 0 To MenuItems.Size - 1
   Dim lblTitle As Label
   Dim lblItem As Label      
   m = MenuItems.Get(i)                    
   m = m.Get("newsitem")                    
   Title = m.Get("title")                    
   Content = m.Get("content")                
   lblTitle.Text = Title
   lblItem.Text = Content
   lblTitle.TextColor = Colors.Black
   lblTitle.TextSize = 12
   lblItem.TextColor = Colors.Black
   lblItem.TextSize = 10
   .
   .
   .
Otherwise the OS tryes to add the same View that already has a Parent View.

Best regards.
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
I believe he also has to initialize them inside the for-next loop and before setting any attributes.
 
Upvote 0
Top