Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
neowinston
Level 7

Populate LabelList from server

I'm trying to populate a LabelList from a server using a JSON file. The parsing works fine but I can't fill out the LabelList. Here is the code and I appreciate your Help.

LabelList code:


<?xml version = "1.0" encoding = "utf-8" ?>

<!--********** Copyright 2016 Roku Corp.  All Rights Reserved. **********-->

<component name = "LabelListExample" extends = "Group" initialFocus = "exampleLabelList" >

  <script type = "text/brightscript" >

    <![CDATA[

    sub init()
      m.top.backgroundURI = "pkg:/images/rsgde_bg_hd.jpg"

      m.programLabel = m.top.findNode("exampleLabelList")

      examplerect = m.programLabel.boundingRect()
      centerx = (1280 - examplerect.width) / 2
      centery = (720 - examplerect.height) / 2
      m.programLabel.translation = [ centerx, centery ]

      m.top.setFocus(true)

      m.getHomeOptionsList = createObject("roSGNode", "getLabelListContent")
      m.getHomeOptionsList.observeField("content", "showhomeoptionslist")
      m.getHomeOptionsList.functionName = "showhomeoptionslist"
      m.getHomeOptionsList.control = "RUN"
    
    end sub

    ]]>

  </script>

  <children >
  
    <LabelList id = "exampleLabelList" translation = "[160,92]" itemSize = "[440,48]">

      <ContentNode role = "content" >
        
      </ContentNode>

    </LabelList>
      
  </children>

</component>



Task code:


<?xml version = "1.0" encoding = "utf-8" ?>
 
<!--********** Copyright 2015 Roku Corp.  All Rights Reserved. **********-->
 
<component name = "getLabelListContent" extends = "Task" >
 
<interface>
  <field id = "uri" type = "uri" />
  <field id = "content" type = "node" />
</interface>
 
<script type = "text/brightscript" >
 
  <![CDATA[
 
  sub init()
    get_programs()
  end sub
  
  sub get_programs() 
    content = createObject("roSGNode", "ContentNode")
    request = CreateObject("roUrlTransfer")
    port = CreateObject("roMessagePort")
    request.SetMessagePort(port)
    
    print "PARSING JSON"
    
    on_demand_url = "http://www.example/example.json" 

    request.SetUrl(on_demand_url)
    
    if (request.AsyncGetToString())
        while (true)
            msg = wait(0, port)
            if (type(msg) = "roUrlEvent")
                code = msg.GetResponseCode()
                if (code = 200)
                    json = ParseJSON(msg.GetString())
                    for each program in json.OnDemand.program
                        print program.name
                        listitem = content.createChild("ContentNode")
                        listitem.title = program.name
                        m.programLabel = listitem
                    end for
                endif
            else if (event = invalid)
                request.AsyncCancel()
            endif
        end while
    endif
    return invalid
End sub


    sub showhomeoptionslist()
    print "This is the callback function!"
    end sub
    
 
  ]]>
 
  </script>
 
</component>
0 Kudos
4 Replies
joetesta
Level 10

Re: Populate LabelList from server

you'll want the callback function (showhomeoptionslist) in your XML's brightscript, not in the Task.
In the task where you currently have "return invaild" you'll want to have "m.top.content = content"
In your showhomeoptionslist callback function which is now moved out of the task node back to the parent brightscript, you'll have 
m.programLabel.content = m.getHomeOptionsList.content
If the content node is constructed as the LabelList expects, it should show up.  If not, check your debugger.
aspiring
0 Kudos
neowinston
Level 7

Re: Populate LabelList from server

Hello Joe,

Thanks for your help!

I've moved showhomeoptionslist() to right below sub init() on the LabelList xml file, but the callback function is not called there. It's only called on the Task itself, and there it gives me an error on the line m.programLabel.content = m.getHomeOptionsList.content.

Error message when showhomeoptionslist() stays on the Task:


'Dot' Operator attempted with invalid BrightScript Component or interface reference. (runtime error &hec) in ...:/components/screens/DetailsScreen/ProgramsJSONParser.xml(67)
m.programLabel.content = m.getHomeOptionsList.content
0 Kudos
joetesta
Level 10

Re: Populate LabelList from server

post your updated code and I'll take a look.  It should run, not in the Task.
aspiring
0 Kudos
neowinston
Level 7

Re: Populate LabelList from server

I got it working!

I just changed getPrograms() to m.top.functionName = "getPrograms" and removed the line m.getHomeOptionsList.functionName = "showhomeoptionslist". So, instead of calling the function directly, I've called it like the docs suggested way and no more crashing. Thanks again for your great help!
0 Kudos