Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Remote JSON table to Tableview Code [PAID]
Started by Tomas_Aboites Sep 10 2018 01:59 PM

1 reply to this topic
json tableview table code paid
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Tomas_Aboites

[GLOBAL: userInfoPane.html]
Tomas_Aboites
  • Enthusiast

  • 50 posts
  • Corona SDK

Hello Corona Developers!

I am in need of code help with creating a Tableview from remote JSON data. 

This will be a paid quick code project that i will pay via Paypal.

Please contact me at:  tom@lvlapps.com

 

Here is what i need modified.....

 

I have a JSON data table online, i need to download that info into Corona, then, create/populate a tableview with specified key/items from the data to show.

I need the item (ex:  first item is "1046") along with "message" value to show in the tableview.

 

Here is the data contained in the json file:

{
"1046":{"date":"September 08, 18","message":"test","pin":"9117","recipient":"test","status":"sent"},
"1048":{"date":"September 03, 18","message":"another testing for the srver.","pin":"1300","recipient":"Usertest", "status":"sent"},
"1057":{"date":"September 05, 18","message":"test","pin":"1097","recipient":"test","status":"sent"},
"1059":{"date":"September 1, 18","message":"testeeec","pin":"2845","recipient":"testet","status":"sent"},
"1060":{"date":"September 03, 18","message":"dfdsfafda","pin":"7303","recipient":"fred","status":"sent"},
"1061":{"date":"September 09, 18","message":"Test","pin":"1010","recipient":"Tester","status":"sent"}
}

and here is the code i have so far. alot of the functionality already works so there wont be too much new code to add.

--SETUP THE TABLEVIEW!!!!!
function createTable()
    local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)

    -- Set color variables depending on theme
    local tableViewColors = {
        rowColor = { default = { 60/255, 60/255, 200/255 }, over = { 60/255, 60/255, 250/255 }},
        lineColor = { 255/255 },
        catColor = { default = { 80/255, 80/255, 255/255, 0.9 }, over = { 80/255, 80/255, 80/255, 0.9 } },
        defaultLabelColor = { 1, 1, 1, 1 },
        catLabelColor = { 1 }
   
    local tableView
    
    -- Text to show which item we selected
    local itemSelected = display.newText( "User selected: ", 0, 0, native.systemFont, 22 )
    itemSelected:setFillColor( unpack(tableViewColors.catLabelColor) )
    itemSelected.x = display.contentWidth+itemSelected.contentWidth
    itemSelected.y = display.contentCenterY
    sceneGroup:insert( itemSelected )
    
    -- Function to return to the tableView
    local function goBack( event )
        transition.to( tableView, { x=display.contentWidth*0.5, time=600, transition=easing.outQuint } )
        transition.to( itemSelected, { x=display.contentWidth+itemSelected.contentWidth, time=600, transition=easing.outQuint } )
        transition.to( event.target, { x=display.contentWidth+event.target.contentWidth, time=480, transition=easing.outQuint } )
    end
    
    -- Back button
    local backButton = widget.newButton {
        width = 128,
        height = 60,
        label = "Back To Tableview",
        onRelease = goBack
    }
    backButton.x = display.contentWidth+backButton.contentWidth
    backButton.y = itemSelected.y+itemSelected.contentHeight+16
    sceneGroup:insert( backButton )
    
    -- Listen for tableView events
    local function tableViewListener( event )
        local phase = event.phase
        --print( "Event.phase is:", event.phase )
    end

----- Handle row rendering
    function onRowRender( event )
        local phase = event.phase
        local row = event.row
        local groupContentHeight = row.contentHeight
            
        local message = display.newText( row, "Message: " .. row.index, 0, 0, "LeagueGothic", 28 )
        message.x = 10
        message.anchorX = 0
        message.y = 25
        message:setFillColor( unpack(row.params.defaultLabelColor) )

    end
    
    -- Handle row updates
    local function onRowUpdate( event )
        local phase = event.phase
        local row = event.row
        --print( row.index, ": is now onscreen" )
    end
    
    -- Handle touches on the row
    local function onRowTouch( event )
        local phase = event.phase
        local row = event.target
        if ( "release" == phase ) or ( "tap" == phase )then
            itemSelected.text = "User selected: " .. row.index
            transition.to( tableView, { x=((display.contentWidth/2)+ox+ox)*-1, time=600, transition=easing.outQuint } )
            transition.to( itemSelected, { x=display.contentCenterX, time=600, transition=easing.outQuint } )
            transition.to( backButton, { x=display.contentCenterX, time=750, transition=easing.outQuint } )
        end
    end
    
-------- Create a tableView ---------------------
    tableView = widget.newTableView
    {
        top = 180-oy,
        left = -ox,
        width = display.contentWidth+ox+ox, 
        height = 420,
        hideBackground = true,
        listener = tableViewListener,
        onRowRender = onRowRender,
        onRowUpdate = onRowUpdate,
        onRowTouch = onRowTouch,
    }
    sceneGroup:insert( tableView )

    -- Create Rows
    for k, v in pairs(user) do
        local isCategory = false
        local rowHeight = 80
        local rowColor = { 
            default = tableViewColors.rowColor.default,
            over = tableViewColors.rowColor.over,
        }

        -- Insert the row into the tableView
        tableView:insertRow
        {
            isCategory = isCategory,
            rowHeight = rowHeight,
            rowColor = rowColor,
            lineColor = tableViewColors.lineColor,
            params = { defaultLabelColor=tableViewColors.defaultLabelColor, catLabelColor=tableViewColors.catLabelColor }
        }
    end 
end --createTable()

--DOWNLOAD THE DATA!!!!!
local jsonFile = function( filename, base )
  if not base then base = system.ResourceDirectory; end

  local path = system.pathForFile( filename, base )
  local contents

  -- io.open opens a file at path. returns nil if no file found
  local file = io.open( path, "r" )
    if file then
      -- read all contents of file into a string
      contents = file:read( "*a" )
      io.close( file ) -- close the file after using it
      end
      return contents
end

function networkListener( event )
  if ( event.isError ) then
    print( "Network error!")
  else

  user = json.decode( event.response )
  -- Go through the array in a loop
     
    for k, v in pairs(user) do
      print("Message ID: "..k)
    end

     timer.performWithDelay( 2000, createTable ) 

  end
end

local json_file_by_get = jsonFile( network.request( "https://linktothejsonfile......", "GET", networkListener ) )

Please let me know if you can fix this and we'll discuss pricing/pay!

 

Tom@LVLapps.com

 



[TOPIC: post.html]
#2

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Contributor

  • 341 posts
  • Corona SDK

This should get you going.

 -- jsonFileDownload.lua   
    
 --[[] 
    --json file on server
 
    {
    "1046":{"date":"September 08, 18","message":"test","pin":"9117","recipient":"test","status":"sent"},
    "1048":{"date":"September 03, 18","message":"another testing for the srver.","pin":"1300","recipient":"Usertest", "status":"sent"},
    "1057":{"date":"September 05, 18","message":"test","pin":"1097","recipient":"test","status":"sent"},
    "1059":{"date":"September 1, 18","message":"testeeec","pin":"2845","recipient":"testet","status":"sent"},
    "1060":{"date":"September 03, 18","message":"dfdsfafda","pin":"7303","recipient":"fred","status":"sent"},
    "1061":{"date":"September 09, 18","message":"Test","pin":"1010","recipient":"Tester","status":"sent"}
    }
--]]

--local PT = require("printReadableTables")
local widget = require("widget")
local M = {}

M.k = "Void"

local sceneGroup = display.newGroup()

local user = {}

--and here is the code i have so far. alot of the functionality already works so there wont be too much new code to add.

    --SETUP THE TABLEVIEW!!!!!
    function createTable()
        local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
     
        -- Set color variables depending on theme
        local tableViewColors = {
            rowColor = { default = { 60/255, 60/255, 200/255 }, over = { 60/255, 60/255, 250/255 }},
            lineColor = { 255/255 },
            catColor = { default = { 80/255, 80/255, 255/255, 0.9 }, over = { 80/255, 80/255, 80/255, 0.9 } },
            defaultLabelColor = { 1, 1, 1, 1 },
            catLabelColor = { 1 },
            }
        local tableView
        
        -- Text to show which item we selected
        local itemSelected = display.newText( "User selected: ", 0, 0, native.systemFont, 22 )
        itemSelected:setFillColor( unpack(tableViewColors.catLabelColor) )
        itemSelected.x = display.contentWidth+itemSelected.contentWidth
        itemSelected.y = display.contentCenterY
        sceneGroup:insert( itemSelected )
        
        -- Function to return to the tableView
        local function goBack( event )
            transition.to( tableView, { x=display.contentWidth*0.5, time=600, transition=easing.outQuint } )
            transition.to( itemSelected, { x=display.contentWidth+itemSelected.contentWidth, time=600, transition=easing.outQuint } )
            transition.to( event.target, { x=display.contentWidth+event.target.contentWidth, time=480, transition=easing.outQuint } )
        end
        
        -- Back button
        local backButton = widget.newButton {
            width = 128,
            height = 60,
            label = "Back To Tableview",
            onRelease = goBack
        }
        backButton.x = display.contentWidth+backButton.contentWidth
        backButton.y = itemSelected.y+itemSelected.contentHeight+16
        sceneGroup:insert( backButton )
        
        -- Listen for tableView events
        local function tableViewListener( event )
            local phase = event.phase
            --print( "Event.phase is:", event.phase )
        end
     
    ----- Handle row rendering
        local function onRowRender( event )
            local phase = event.phase
            local row = event.row
            local groupContentHeight = row.contentHeight
                
          --[[]  local message = display.newText( row, "Message: " .. row.index, 0, 0, "LeagueGothic", 28 )
            message.x = 10
            message.anchorX = 0
            message.y = 25
            message:setFillColor( unpack(row.params.defaultLabelColor) )  --]]
            
                local message = display.newText( row, "Message "..M.k..": " ..user[M.k].message, 0, 0, "LeagueGothic", 28 )
                message.x = 10
                message.anchorX = 0
                message.y = 25
                message:setFillColor( unpack(row.params.defaultLabelColor) )

        end
        
        -- Handle row updates
        local function onRowUpdate( event )
            local phase = event.phase
            local row = event.row
            --print( row.index, ": is now onscreen" )
        end
        
        -- Handle touches on the row
        local function onRowTouch( event )
            local phase = event.phase
            local row = event.target
            if ( "release" == phase ) or ( "tap" == phase )then
                itemSelected.text = "User selected: " .. row.index
                transition.to( tableView, { x=((display.contentWidth/2)+ox+ox)*-1, time=600, transition=easing.outQuint } )
                transition.to( itemSelected, { x=display.contentCenterX, time=600, transition=easing.outQuint } )
                transition.to( backButton, { x=display.contentCenterX, time=750, transition=easing.outQuint } )
            end
        end
        
    -------- Create a tableView ---------------------
        tableView = widget.newTableView
        {
            top = 180-oy,
            left = -ox,
            width = display.contentWidth+ox+ox, 
            height = 420,
            hideBackground = true,
            listener = tableViewListener,
            onRowRender = onRowRender,
            onRowUpdate = onRowUpdate,
            onRowTouch = onRowTouch,
        }
        sceneGroup:insert( tableView )
     
        -- Create Rows
        for k, v in pairs(user) do
            M.k = k
            local isCategory = false
            local rowHeight = 80
            local rowColor = { 
                default = tableViewColors.rowColor.default,
                over = tableViewColors.rowColor.over,
            }
     
            -- Insert the row into the tableView
            tableView:insertRow
            {
                isCategory = isCategory,
                rowHeight = rowHeight,
                rowColor = rowColor,
                lineColor = tableViewColors.lineColor,
                params = { defaultLabelColor=tableViewColors.defaultLabelColor, catLabelColor=tableViewColors.catLabelColor }
            }
        end 
    end --createTable()
     
    --DOWNLOAD THE DATA!!!!!
    local jsonFile = function( filename, base )
      if not base then base = system.ResourceDirectory; end
     
      local path = system.pathForFile( filename, base )
      local contents
     
      -- io.open opens a file at path. returns nil if no file found
      local file = io.open( path, "r" )
        if file then
          -- read all contents of file into a string
          contents = file:read( "*a" )
          io.close( file ) -- close the file after using it
          end
          return contents
    end
     
    function networkListener( event )
        print("networkListener is called")
        
      if ( event.isError ) then
        print( "Network error!")
       
      else
      user = nil
      
      user = json.decode( event.response )
      -- Go through the array in a loop
      
     -- print("user table prints below")
    --  PT.print_r(user)
     -- print("")
         
        for k, v in pairs(user) do
          print("Message ID: "..k)
        end
     
         timer.performWithDelay( 2000, createTable ) 
     
      end
    end
    
    
function  M.getJsonFile()
    print("M.getJsonFile() is called")
    
    local link = "http://mylink.com/Test1.json"
     
    local json_file_by_get = jsonFile( network.request( "https://linktothejsonfile......", "GET", networkListener ) )
   -- local json_file_by_get = jsonFile( network.request( link, "GET", networkListener ) )

end


--I called this function from my main.lua
M.getJsonFile()
return M

M.k references the table key as you iterate over your table so you can get whatever you want and insert it where ever you need.

 

Hope this helps,

 

Nail




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: json, tableview, table, code, paid